Java Methods – Create Reusable Code with Simple Examples
Java Programming

Java Methods – Create Reusable Code with Simple Examples

25 Sep, 2026 CCI Admin Java Programming
Back to All Blogs

Java Methods – Create Reusable Code with Simple Examples

Published: 25 Sep, 2026 | Author: CCI Admin | Category: Java / Programming

In Java programming, methods are used to organize code into reusable blocks. Instead of writing the same code again and again, we can create a method and call it whenever we need it.

What is a Method in Java?

A method is a block of code that performs a specific task. A method runs when it is called from another part of the program.

Methods make programs easier to understand, reuse, test, and maintain.

Basic Syntax of a Java Method


returnType methodName() {
    // code to execute
}

For example:


void greet() {
    System.out.println("Welcome to Java!");
}

Calling a Method

After creating a method, we can call it using its method name.


public class Main {

    static void greet() {
        System.out.println("Welcome to Java!");
    }

    public static void main(String[] args) {
        greet();
    }
}

Output:


Welcome to Java!

Method with Parameters

A method can receive values through parameters. This allows us to use the same method with different data.


static void greetStudent(String name) {
    System.out.println("Hello " + name);
}

public static void main(String[] args) {
    greetStudent("Arun");
    greetStudent("Priya");
}

Output:


Hello Arun
Hello Priya

Method with Return Value

A method can return a value using the return keyword.


static int add(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    int result = add(10, 20);
    System.out.println(result);
}

Output:


30

Why Use Methods in Java?

  • Methods help create reusable code.
  • They reduce code duplication.
  • They make programs easier to understand.
  • They make debugging easier.
  • They improve code organization.

Real-Life Example

Imagine a student management system. Instead of writing separate code for calculating marks for every student, we can create one method and reuse it.


static int calculateTotal(int mark1, int mark2, int mark3) {
    return mark1 + mark2 + mark3;
}

public static void main(String[] args) {
    int total = calculateTotal(80, 75, 90);
    System.out.println("Total Marks: " + total);
}

Output:


Total Marks: 245

Method Types – Quick View

Type Example
Without parameter and return value void greet()
With parameters void greet(String name)
With return value int add(int a, int b)

Frequently Asked Questions

1. What is a method in Java?

A method is a reusable block of code designed to perform a specific task.

2. Why are methods useful?

Methods reduce duplicate code and make Java programs easier to organize and maintain.

3. Can a Java method return a value?

Yes. A method can return a value using the return keyword.

4. Can a method accept input?

Yes. Methods can accept input through parameters.

Conclusion

Java methods are an important part of programming because they help developers create reusable, organized, and maintainable code. Beginners should practice creating methods with parameters and return values to build a strong foundation in Java programming.

Related CCI Blogs


Contact CCI Computer Education – Pudukkottai

Want to learn JavaScript, Web Development, and other programming skills through practical training?

Call: 98427 97945  |  Website: ccipudukkottai.com

WhatsApp Channel: Join Now

Share: Facebook | WhatsApp | Twitter