Methods group reusable logic inside a class. They declare access modifier, optional static, return type, name, parameters, and body—similar to PHP methods but with mandatory return types (or void).
Definition and call
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(2, 3));
}
Overloading
Same method name, different parameter lists—resolved at compile time by signature.
Important interview questions and answers
- Q: Method overloading vs overriding?
A: Overloading is same class, different params; overriding is subclass replacing superclass method. - Q: Pass by value in Java?
A: Always—references copy the pointer, not the object itself.
Self-check
- What keyword means the method returns nothing?
- Can two methods share a name if parameters differ?