Method
A method is a named block of code inside a class that performs a task. It optionally takes parameters and returns a value (or void). Methods are how objects and classes expose their behaviour.
Anatomy
public int add(int a, int b) { // public = modifier, int = return type, add = name, (int a, int b) = params
return a + b;
}
Instance vs static methods
public class Calc {
public int value = 0;
public void addTo(int n) { value += n; } // instance method — uses 'this'
public static int square(int n) { return n * n; } // static — belongs to the class
}
Calc c = new Calc();
c.addTo(5); // call instance method on an object
int sq = Calc.square(9); // call static method on the class, no instance
Overloading
Multiple methods can share a name if their parameter lists differ:
int max(int a, int b) { return a > b ? a : b; }
double max(double a, double b) { return a > b ? a : b; }
Varargs
public int sum(int... nums) {
int total = 0;
for (int n : nums) total += n;
return total;
}
sum(1, 2, 3, 4, 5); // 15
See the full method guide for overriding, visibility, and naming conventions.