nerdexam
Oracle

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…

Java Basics

Question

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++; System.out.println(StaticField.i + " " + obj.i); } }

Options

  • A10 10
  • B8 9
  • C9 8
  • D7 10

How the community answered

(25 responses)
  • A
    80% (20)
  • B
    12% (3)
  • C
    4% (1)
  • D
    4% (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.i and StaticField.i are separate variables that track independently - they're not.
  • D (7 10) wrongly treats StaticField.i as unchanged while only obj.i increments - 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

#Static fields#Class variables#Variable scope#Post-increment operator

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice