Method Body
1. What Is the Body? (beginner)
Body = { ... } block after prototype where work happens: locals, logic, return. Prototype promises, body delivers!
public static int add(int a, int b) { // prototype +
int sum = a + b; // 1. locals (temp, die after!)
if (sum < 0) { // 2. logic (if/loops/calls!)
return 0;
}
return sum; // 3. return (must match type on EVERY path!)
} // body ends
4. Scope Inside Body
Params + locals live in method frame (die at }!). Same name shadows field (use this.x — next topics!).
5. Exam Points
- Body parts + missing-return error — 5 marks, frequent.
- Unreachable after
return— 2-3 marks, trap.