AP CSA Practice Test Static Methods Variables
AP CSA Practice Test: Static Methods and Variables
Unit 3: Class Creation | 10 Questions | AP Computer Science A
public class Tracker {
private static int count = 0;
public Tracker() {
count++;
}
public static int getCount() {
return count;
}
}
// In main:
Tracker t1 = new Tracker();
Tracker t2 = new Tracker();
Tracker t3 = new Tracker();
System.out.println(Tracker.getCount());
Explanation: count is static, shared across ALL instances. Each constructor increments it. After three objects are created, count = 3.
Common Mistake: Thinking each object has its own count. Static variables are shared by all instances.
public class Demo {
private int x = 5;
private static int y = 10;
public static void show() {
System.out.println(x); // Line A
System.out.println(y); // Line B
}
}
Explanation: Static methods cannot access instance variables. x is an instance variable, so accessing it from the static method show() causes a compile error. y is static, so Line B is fine.
Common Mistake: Forgetting that static methods have no 'this' reference and cannot access instance state.
compute in class Helper?Helper h = new Helper(); h.compute();
Helper.compute();
compute();
Explanation: Both calling through an instance (A) and the class name (B) compile. However, calling static methods through the class name is the correct convention and avoids confusion. B is the standard practice.
Common Mistake: Thinking you must create an object to call static methods. You can, but you shouldn't.
public class Val {
private static int s = 0;
private int n;
public Val(int n) {
this.n = n;
s += n;
}
public static int getSum() {
return s;
}
}
// In main:
Val v1 = new Val(2);
Val v2 = new Val(3);
Val v3 = new Val(5);
System.out.println(Val.getSum());
Explanation: Each constructor adds n to the static variable s: 0+2=2, 2+3=5, 5+5=10. Since s is static, all modifications accumulate.
Common Mistake: Thinking s resets for each object. Static variables persist across all instances.
static.I. A static method can call another static method in the same class
II. A static method can create objects of its own class
III. A static method can use the
this keywordExplanation: I: true, static methods can call other static methods. II: true, static methods can use new to create objects (that is what main does). III: false, this refers to the current instance, but static methods have no instance.
Common Mistake: Thinking static methods can't create objects. main() is static and always creates objects.
public class MathUtil {
public static int square(int n) {
return n * n;
}
public static int cube(int n) {
return n * square(n);
}
}
// In main:
System.out.println(MathUtil.cube(3));
Explanation: cube(3) returns 3 * square(3) = 3 * 9 = 27. Static methods can call other static methods in the same class without the class name prefix.
Common Mistake: Thinking static methods need ClassName. prefix to call each other within the same class.
public class ID {
private static int next = 100;
private int id;
public ID() {
id = next++;
}
public int getId() {
return id;
}
}
// In main:
ID a = new ID();
ID b = new ID();
System.out.println(a.getId() + " " + b.getId());
Explanation: First object: id = next++ assigns 100 to id, then increments next to 101. Second object: id = 101, next becomes 102. Output: 100 101.
Common Mistake: Confusing prefix (++next) with postfix (next++). Postfix assigns first, then increments.
static instead of instance?Explanation: Static methods are for operations that don't depend on a specific object's state. Examples: Math.sqrt(), Integer.parseInt(). If a method needs instance data, it should be an instance method.
Common Mistake: Using static out of convenience. Static should only be used for class-level operations.
public class Counter {
private static int total = 0;
private int mine = 0;
public void add() {
total++; mine++;
}
public String toString() {
return mine + "/" + total;
}
}
// In main:
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.add(); c1.add();
c2.add();
System.out.println(c1);
System.out.println(c2);
Explanation: total is static (shared): 3 total adds. mine is instance (per-object): c1 has 2, c2 has 1. c1: "2/3". c2: "1/3".
Common Mistake: Thinking total is separate for each object. Static means one shared value.
Demo.update() twice?public class Demo {
private static int val = 1;
public static int update() {
val *= 2;
return val;
}
}
Explanation: Static variable val persists between calls. First call: val = 1*2 = 2, returns 2. Second call: val = 2*2 = 4, returns 4.
Common Mistake: Thinking val resets between calls. Static variables keep their value until explicitly changed.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]