1Z0-808 · Question #4
class X { static int i; int j; public static void main(String[] args) { X x1 = new X(); X x2 = new X(); x1.i = 3; x1.j = 4; x2.i = 5; x2.j = 6; System.out.println( "x1.i = " + x1.i + " " + "x1.j = "…
The correct answer is C. 5 4 5 6. Option C (5 4 5 6) is correct because i is a static variable, meaning it belongs to the class itself, not to any individual object. When x2.i = 5 executes, it overwrites the earlier x1.i = 3 assignment - both references point to the same single memory location. Since j is an…
Question
Options
- A3 4 5 6
- B3 4 3 6
- C5 4 5 6
- D3 6 4 6
How the community answered
(25 responses)- A4% (1)
- B8% (2)
- C76% (19)
- D12% (3)
Explanation
Option C (5 4 5 6) is correct because i is a static variable, meaning it belongs to the class itself, not to any individual object. When x2.i = 5 executes, it overwrites the earlier x1.i = 3 assignment - both references point to the same single memory location. Since j is an instance variable, each object keeps its own copy: x1.j stays 4 and x2.j stays 6.
Why the distractors fail:
- A (3 4 5 6) - wrongly assumes x1.i retains 3 after x2.i is set; ignores that static fields are shared across all instances.
- B (3 4 3 6) - also misreads static sharing, and incorrectly shows x2.i as 3 rather than the final assigned value of 5.
- D (3 6 4 6) - appears to swap the j values and misapplies static behavior entirely; no assignment ever puts 3 or 4 into i at the point of printing.
Memory tip: Think "Static = Shared, Instance = Individual." Whenever you assign to a static field - regardless of which reference you use (x1.i or x2.i) - you're writing to one class-level slot, so the last write always wins for everyone.
Topics
Community Discussion
No community discussion yet for this question.