CODE STRUCTURE

Java methods

A method is a named code block that runs only when called. Methods split a program into clear parts and let you reuse the same code many times.

What are methods for?

Methods are also called functions. They help you:

  • use an action defined once in several parts of a program;
  • avoid repeated code;
  • break a large task into smaller actions;
  • give an action a clear name;
  • test and change the program more easily.

Java provides built-in methods, such as System.out.println(), but you can also create your own.

Creating a method

A method is declared inside a class. The method name is followed by parentheses (), and its code goes inside braces:

public class Main {
    static void ruostiPusrycius() {
        // Executable code goes here
    }
}
There is no console output.

In this example, we have created the method but have not called it. It also does not yet contain println().

  1. The method is written inside the class Main , outside any other method.
  2. static in this example allows the method to be called directly from main()without creating an object.
  3. void means the method performs an action but returns no value.
  4. ruostiPusrycius is a clear method name describing the action.
  5. Empty parentheses () means the method does not yet require any input data.
Parts of a method declaration
PartMeaning
staticThe method belongs to the class, so in this example it can be called without creating an object
voidThe method returns no value
ruostiPusryciusThe method name, describing its action
()Parameter positions; this method has none

Calling a method

Declaring a method is not enough: you must call it to run its code. Write its name, parentheses and a semicolon:

public class Main {
    static void pasisveikinti() {
        System.out.println("Labas, Ona!");
    }

    public static void main(String[] args) {
        pasisveikinti();
    }
}
Console output

Labas, Ona!

  1. Java starts running the program in the method main().
  2. It reaches the line pasisveikinti();. This is a call to our method.
  3. Java temporarily enters the method pasisveikinti() body.
  4. There it executes println(), so the console displays Labas, Ona!.
  5. When the method finishes, Java returns to main() where it left off.

Method execution order

public class Main {
    static void plautiRankas() {
        System.out.println("Plauname rankas");
    }

    public static void main(String[] args) {
        System.out.println("Grįžome namo");
        plautiRankas();
        System.out.println("Galime sėsti prie stalo");
    }
}
Console output

Grįžome namo
Plauname rankas
Galime sėsti prie stalo

  1. main() first displays the text Grįžome namo.
  2. Next, it calls the method plautiRankas().
  3. Java enters that method and displays Plauname rankas.
  4. When it finishes, the program returns to the next main() line.
  5. Finally, the console displays Galime sėsti prie stalo. The method thus runs exactly where it is called.

Reusing a method

You can call the same method as often as needed without copying its internal code:

public class Main {
    static void primintiAtsigerti() {
        System.out.println("Laikas atsigerti vandens");
    }

    public static void main(String[] args) {
        primintiAtsigerti();
        primintiAtsigerti();
        primintiAtsigerti();
    }
}
Console output

Laikas atsigerti vandens
Laikas atsigerti vandens
Laikas atsigerti vandens

  1. The method primintiAtsigerti() contains the text only once.
  2. In the method main() we call the same method three times.
  3. Each call runs all the code inside the method again.
  4. The same reminder therefore appears in the console three times.
  5. To change the reminder text, we would only need to edit one line inside the method.

Method names

A method name usually starts with a lowercase letter and uses camelCase . Since a method describes an action, it helps to start with a verb, for example, ruostiPusrycius(), skaiciuotiKaina() or rodytiPranesima().

Parameters and return values

Data can be passed to a method through parameters. Instead of void , you can also specify a return type and return the result using return.