AP CSA Unit 3 Day 2: Instance vs. Static
Share
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?
What is the output?
public class mystery {
private static int val = 0;
private int id;
public mystery() {
val++;
id = val;
}
public int getID() {
return id;
}
}
// In another class:
mystery c1 = new mystery();
mystery c2 = new mystery();
mystery c3 = new mystery();
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
Ready for More Challenges?
Cycle 2 questions prepare you for the hardest AP CSA exam questions.
Study Games Practice FRQs