📜 WAYPOINT LESSON
Method anatomy
⭐ beginner⏳ 14 min read📍 Lesson 21 of 256
Headers, parameters, returns, void, early return, argument matching.
A method is a named, reusable block of behavior. The header tells you everything:
public static double average(int a, int b)
// ↑static ↑return type ↑name ↑parameters
- Parameters are the inputs — typed placeholders filled by the call.
- Return type: what comes out.
voidmeans "returns nothing".
public static double average(int a, int b) {
return (a + b) / 2.0;
}
return does two jobs: hand back the value and exit immediately. Code
after a taken return never runs — which is why early-return validation
works:
public static String sign(int n) {
if (n < 0) {
return "neg";
}
return "non-neg";
}
A void method can still return; early — bare, with no value. Arguments
are matched by position and type: average(3, 4) fills a = 3, b = 4.
Too few, too many, or mismatched types = compile error.