Java Methods

A Java method is a named block of code that runs when you call it. Methods help you organize a program into smaller parts and reuse the same logic without writing it again.

You have already used the main method. It is the method Java starts with when running a program.

Create And Call A Method

A method has a return type, a name, parentheses, and a body inside braces. In the examples in this lesson, methods are marked static so they can be called directly from the static main method.

public class Main {
    static void greet() {
        System.out.println("Hello from a method!");
    }

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

Output:

Hello from a method!
Hello from a method!

The method is named greet. It does not run when Java reads its definition. It runs only when the program calls greet();.

Method Syntax

A simple method follows the pattern static returnType methodName(), followed by a block of code in braces. For example, static void greet() declares a method named greet that does not return a value.

static means the method belongs to the class itself. void means the method does not send a value back to the caller. The parentheses are required, even when the method has no parameters.

Parameters

A parameter is a variable listed inside a method’s parentheses. It lets the caller pass information into the method.

public class Main {
    static void printScore(String name, int score) {
        System.out.println(name + " scored " + score);
    }

    public static void main(String[] args) {
        printScore("Mia", 92);
        printScore("Noah", 85);
    }
}

Output:

Mia scored 92
Noah scored 85

In this example, name and score are parameters. The values "Mia", 92, "Noah", and 85 are arguments, because they are the actual values passed during the method calls.

Return Values

A method can calculate a value and send it back with return. When a method returns a value, its return type must match the kind of value it returns.

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

    public static void main(String[] args) {
        int total = add(6, 4);
        System.out.println(total);
        System.out.println(add(10, 5));
    }
}

Output:

10
15

The add method returns an int, so Java lets you store the result in an int variable or print it directly. Once a return statement runs, the method ends.

Void Methods

Use void when a method should perform an action but does not need to return a value. Printing text is a common beginner example of a void method.

public class Main {
    static void showMenu() {
        System.out.println("1. Start");
        System.out.println("2. Settings");
        System.out.println("3. Exit");
    }

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

Output:

1. Start
2. Settings
3. Exit

A void method can still use return; by itself to leave the method early, but it cannot return a value.

Why Use Methods?

  • Methods make code easier to read because each method can describe one task.
  • Methods reduce repetition when the same task is needed more than once.
  • Methods make changes easier because you can update one method instead of many copied lines.
  • Methods let you break a larger problem into smaller steps.

Common Mistakes

  • Forgetting the parentheses when calling a method, such as writing greet; instead of greet();.
  • Returning the wrong type, such as returning text from a method declared with int.
  • Trying to store the result of a void method in a variable.
  • Calling a non-static method directly from main before learning about objects.

Takeaway: Java methods give a name to a reusable block of code, and they can use parameters and return values to work with different data.