Methods

Basic Java
≈ 4 min read

Why methods are useful

When the same sequence of actions is needed several times, copying it throughout the program is inconvenient. A method lets you give that action a name, move the code into a separate block, and call it wherever it is needed.

static void printGreeting() {
    System.out.println("Привет!");
}

printGreeting();
printGreeting();

The code inside printGreeting() is written once, but it can be executed as many times as needed. This makes the program shorter and makes individual actions easier to recognize.

For now, treat static as part of the example's form. It is used here so that methods like these can be used alongside the familiar main. The modifier itself will be covered later.

Declaring and calling a method

A simple method declaration specifies the result type, the name, parentheses, and the body inside curly braces. If a method does not return anything, void is written instead of a result type.

static void sayHello() {
    System.out.println("Hello");
}

This is only a declaration: it tells Java what the method should do. For the code inside to run, the method must be called:

sayHello();

When the method is called, the program moves into the method body, executes its statements, and then continues from the line after the call.

Parameters and arguments

A method can receive data from the outside. Its declaration uses parameters for this — variables that are available inside the method.

static void printSum(int a, int b) {
    System.out.println(a + b);
}

When the method is called, concrete values are passed in place of the parameters — these are arguments:

printSum(4, 7);
printSum(10, 20);

The same method can work with different data. The number and types of arguments must match the method's parameters.

Returning a result

Often a method should not print the answer itself, but calculate a value and give it back to the calling code. In that case, a result type is specified instead of void, and the value is returned with return.

static int square(int number) {
    return number * number;
}

int result = square(5);
System.out.println(result);

The result type of square() is int, so the result of the call can be stored in an int variable or used directly in an expression.

return ends the current method call. Code in that method after an executed return is not executed.

Local variables

Parameters and variables declared inside a method belong to that method call. They cannot be accessed by name from outside the method.

static int doubleNumber(int number) {
    int result = number * 2;
    return result;
}

Here, number and result exist only inside doubleNumber(). This lets each method work with its own temporary data without cluttering the rest of the program with unnecessary variables.

Method overloading

In Java, several methods can have the same name if their parameter lists are different. This is called overloading.

static int max(int a, int b) {
    return a > b ? a : b;
}

static int max(int a, int b, int c) {
    return max(max(a, b), c);
}

When a method is called, Java selects the matching version based on the arguments that were passed. So max(3, 8) calls the first method, while max(3, 8, 5) calls the second. Changing only the return type is not enough to overload a method.

Split the program into clear actions

A good method usually performs one clear task and receives only the data it actually needs. Then the main code reads like a sequence of actions, while the details of each action are hidden inside a method.

int price = 1200;
int discount = 200;

int finalPrice = calculateFinalPrice(price, discount);
System.out.println(finalPrice);

The name calculateFinalPrice() immediately explains the intent of the code. That is one of the main values of methods: they not only let you reuse statements, but also split a program into small, meaningful parts.