Unit 3 Cycle 2 Day 13: I/II/III: Static Behavior
Share
I/II/III: Static Behavior
Section 3.7 — Static Variables
Key Concept
I/II/III static behavior questions test whether you understand that static variables are shared across all instances, static methods cannot use this, and changes to static fields through one reference are visible through all references. Each statement might claim: a static method can access instance variables (false), a static variable has a different value for each object (false), or calling a static method through an object reference is valid but discouraged (true). Evaluate each based on static semantics.
Consider the following class.
After executing Tracker a = new Tracker(); Tracker b = new Tracker();, which statements are true?
I. a.getId() returns 1
II. b.getId() returns 2
III. Tracker.getTotal() returns 2
Answer: (C) I, II, and III
I: TRUE. First constructor: total becomes 1, id=1.
II: TRUE. Second constructor: total becomes 2, id=2.
III: TRUE. total is 2 after two constructions.
Why Not the Others?
(A) III is also true. total is a static variable shared across all instances, and it equals 2.
(B) I is also true. The first Tracker created gets id=1.
(D) I and II are also true. Each Tracker gets a unique id based on the static counter.
Common Mistake
Static variables are shared. Each new Tracker() increments the shared total, but each object stores its own id at the time of creation.
AP Exam Tip
Static counters in constructors create auto-incrementing IDs. The static variable is shared (total), while the instance variable (id) captures the current total at construction time.