1Z0-808 · Question #68
What is the result? class StaticField { static int i = 7; public static void main(String[] args) { StaticField obj = new StaticField(); obj.i++; StaticField.i++; obj.i++…
The correct answer is A. 10 10. Option A (10 10) is correct because i is a static field, meaning it belongs to the class itself - not to any individual object. Both obj.i and StaticField.i are simply two different ways of referencing the exact same memory location, so every increment (regardless of how it's…
Question
Options
- A10 10
- B8 9
- C9 8
- D7 10
How the community answered
(25 responses)- A80% (20)
- B12% (3)
- C4% (1)
- D4% (1)
Explanation
Option A (10 10) is correct because i is a static field, meaning it belongs to the class itself - not to any individual object. Both obj.i and StaticField.i are simply two different ways of referencing the exact same memory location, so every increment (regardless of how it's written) modifies the single shared value: 7 → 8 → 9 → 10.
Why the distractors fail:
- B (8 9) and C (9 8) wrongly assume
obj.iandStaticField.iare separate variables that track independently - they're not. - D (7 10) wrongly treats
StaticField.ias unchanged while onlyobj.iincrements - again, impossible since they alias the same field.
Memory tip: Think of a static field as a single whiteboard on the wall of the classroom (the class). Every student (instance) in the room sees and writes on the same whiteboard - it doesn't matter whether you say "the board" or point to it through a student's desk, you're always changing the one shared value.
Topics
Community Discussion
No community discussion yet for this question.