Method (static)
Try it yourself →Function (method) definition.
Syntax
static returnType name(type p) { return ...; }Description
Return type, name, and parameters are written. Must be static if called from main.
Example
public class Main {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
System.out.println(square(5));
}
}Expected output
25
When to use?
Preventing code repetition.
Common mistakes
- ⚠️ void return type means 'returns nothing'.