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
}
}In this example, we have created the method but have not called it. It also does not yet contain println().
- The method is written inside the class
Main, outside any other method. staticin this example allows the method to be called directly frommain()without creating an object.voidmeans the method performs an action but returns no value.ruostiPusryciusis a clear method name describing the action.- Empty parentheses
()means the method does not yet require any input data.
| Part | Meaning |
|---|---|
static | The method belongs to the class, so in this example it can be called without creating an object |
void | The method returns no value |
ruostiPusrycius | The 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();
}
}Labas, Ona!
- Java starts running the program in the method
main(). - It reaches the line
pasisveikinti();. This is a call to our method. - Java temporarily enters the method
pasisveikinti()body. - There it executes
println(), so the console displays Labas, Ona!. - 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");
}
}Grįžome namo
Plauname rankas
Galime sėsti prie stalo
main()first displays the text Grįžome namo.- Next, it calls the method
plautiRankas(). - Java enters that method and displays Plauname rankas.
- When it finishes, the program returns to the next
main()line. - 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();
}
}Laikas atsigerti vandens
Laikas atsigerti vandens
Laikas atsigerti vandens
- The method
primintiAtsigerti()contains the text only once. - In the method
main()we call the same method three times. - Each call runs all the code inside the method again.
- The same reminder therefore appears in the console three times.
- 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.