Defining & calling methods in Java
Modifiers, return type, name, parameters, body.
A named recipe
A method is a named block of code you can run whenever you need it. Define it once, then call it by writing its name with arguments in parentheses. Each call runs the body once, with the parameter set to that call's value.
static void greet(String name) {
System.out.println("Hi " + name);
}
void main() {
greet("Ana"); // Hi Ana
greet("Ben"); // Hi Ben
}Reading the header
public static int max(int a, int b) reads left to right: **public is an access modifier (who may call it), static means it belongs to the class (more soon), int is the return type, max the name, and (int a, int b)** the parameter list. The body in braces does the work.
public static int max(int a, int b) {
return a > b ? a : b;
}Calls inside calls
What prints?
static int square(int x) {
return x * x;
}
static int plusOne(int x) {
return x + 1;
}
void main() {
System.out.println(square(plusOne(2)));
}596
Show the answer
The inner call runs first: plusOne(2) is 3. Then square(3) is 9. Read nested calls from the inside out.
Parentheses and counts
A call always needs parentheses, even with no parameters: greet(). Without them, Java looks for a *variable* named greet. And the arguments must match the parameters in number and type. The compiler checks every call before the program runs.
static void greet() { }
static int add(int a, int b) { ... }
greet; // error: not a variable
add(1, 2, 3); // error: needs two ints
add(1, "two"); // error: wrong typeYour turn
What happens?
static int add(int a, int b) {
return a + b;
}
void main() {
System.out.println(add(5));
}Prints 5Compile errorThrows IllegalArgumentException
Show the answer
Compile error. add needs exactly two ints, and the compiler checks the call against the parameter list. A mismatch never reaches runtime.
Where methods live
void main() {
int helper() {
return 1;
}
}Java doesn't allow declaring a method inside another method: compile error.
static int helper() {
return 1;
}
void main() {
System.out.println(helper());
}Methods are declared in a class (or at the top level of a compact source file), next to each other.
In real projects
Good methods are the building blocks of every codebase. A well-named method like calculateShipping(order) documents itself, can be tested on its own, and is fixed in one place. Code reviewers routinely ask to split a 200-line method into smaller ones.
Key takeaways
- Header: modifiers returnType name(parameters)
- Parentheses are required, even when there are no parameters
- The arguments must match the parameters in number and type
- Methods are declared in a class, never inside another method
💡 A method is like a recipe card: write it once, then 'cook' it any time by calling its name with the ingredients.
Since Java 25, a file can start straight with void main(): no class, no public static, no String[] args. Earlier versions demanded public static void main(String[] args) inside a class.
Practice questions
What does this print?
static void greet(String name) {
System.out.println("Hi " + name);
}
void main() {
greet("Ana");
greet("Ben");
}- Hi Ana
- Hi name Hi name
- Hi Ana Hi Ben
- Compile error
Check your answer
Hi Ana Hi Ben. Each call runs the body once, with name set to that call's argument.
What does this print?
static int twice(int x) {
return x * 2;
}
static int plusOne(int x) {
return x + 1;
}
void main() {
System.out.println(twice(plusOne(3)));
}- 7
- 8
- 6
- 14
Check your answer
8. The inner call runs first: plusOne(3) is 4. Then twice(4) is 8.