Unit 3 Cycle 2 Day 2: Instance vs Static

Unit 3, Classes & Objects • Cycle 2
Day 2 Advanced Practice • Harder Difficulty
Focus: Instance vs Static Hard Instance vs Static

Advanced Practice Question

Format: Instance vs Static

What is the output?
public class Counter {
    private static int total = 0;
    private int id;
    
    public Counter() {
        total++;
        id = total;
    }
    
    public int getID() {
        return id;
    }
}

// In another class:
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println(c2.getID());
Difficulty: Hard  |  Topic: Instance vs Static  |  Cycle: 2 (Advanced)
Why This Answer?

total is static (shared). First object: total becomes 1, id=1. Second object: total becomes 2, id=2. Third object: total becomes 3, id=3. c2.getID() returns 2.

Common Mistake
Watch Out!

Not understanding that static variables are shared across ALL objects.

AP Exam Strategy

Static = shared. Each new object increments the SAME total variable.

Master This Topic

This Cycle 2 HARD question tests instance vs static. Review Unit 3 concepts to build mastery of classes & objects.

  • Understanding instance vs static
  • Tracing code execution accurately
  • Avoiding common pitfalls
View Unit 3 Study Guide

Ready for More Challenges?

Cycle 2 questions prepare you for the hardest AP CSA exam questions.

Study Games Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.