Static vs Instance Variables in AP CSA: Complete Guide (2025-2026)
Static vs Instance Variables in AP CSA: Complete Guide (2025-2026)
Understanding the difference between static (class-level) and instance (object-level) variables is one of the most-tested Unit 3 concepts on the AP CSA exam. Static variables are shared across all objects; instance variables are unique to each object. Getting this wrong in a trace or FRQ can cascade into several lost points.
Table of Contents
Instance Variables
- Declared without the
statickeyword inside a class but outside any method - Each object gets its own separate copy
- Accessed via
this.fieldor justfieldinside instance methods - Destroyed when the object is garbage-collected
Static Variables and Methods
- Declared with the
statickeyword - Shared by all instances — one copy exists regardless of how many objects are created
- Accessed via
ClassName.field(preferred) or via any object reference - Static methods: no implicit
this, cannot access instance variables directly - Common use: counters, constants shared across all objects
On the 2025-2026 exam: static vs instance is Unit 3 content (10–18%). The most common MCQ scenario is a class that uses a static counter to track how many objects have been created, then asks what value each object sees.
Code Examples
Example 1: Static Counter Pattern
public class Ticket {
private static int nextId = 1; // shared across all Tickets
private int ticketId; // unique per Ticket
public Ticket() {
ticketId = nextId;
nextId++;
}
public int getId() { return ticketId; }
public static int getNextId() { return nextId; }
public static void main(String[] args) {
Ticket t1 = new Ticket();
Ticket t2 = new Ticket();
Ticket t3 = new Ticket();
System.out.println(t1.getId() + " " + t2.getId() + " " + t3.getId());
System.out.println("Next will be: " + Ticket.getNextId());
}
}
Example 2: Static Method Cannot Access Instance Variable
public class Converter {
private double ratio = 2.54;
// This method is static but tries to use ratio (instance var)
// public static double convert(double inches) {
// return inches * ratio; // COMPILE ERROR
// }
// Fixed: make ratio static, or pass it as parameter
private static final double CM_PER_IN = 2.54;
public static double convert(double inches) {
return inches * CM_PER_IN;
}
public static void main(String[] args) {
System.out.println(Converter.convert(12.0));
}
}
Example 3: Shared vs Per-Object State
public class Fleet {
private static int totalShips = 0; // class-level
private int crew; // per-object
public Fleet(int crew) {
this.crew = crew;
totalShips++;
}
public static void main(String[] args) {
Fleet f1 = new Fleet(50);
Fleet f2 = new Fleet(30);
System.out.println("Total ships: " + Fleet.totalShips);
System.out.println("f1 crew: " + f1.crew);
System.out.println("f2 crew: " + f2.crew);
}
}
Common Pitfalls
Static methods have no this reference. They cannot directly access instance fields. The fix: accept the object as a parameter, or make the field static if it truly should be shared.
Static variables do NOT reset when new objects are created. A static counter continues accumulating across all constructor calls.
When two objects share a static variable and one modifies it, the change is visible to all. When an instance variable is modified, only that object is affected.
obj.staticMethod() compiles but is considered poor style. Always use ClassName.staticMethod() to make the static nature clear.
Check for Understanding
public class Widget {{
private int serialNum;
private static int count = 0;
public Widget() {{
count++;
serialNum = count;
}}
}}
Widget a = new Widget();
Widget b = new Widget();
Widget c = new Widget();
System.out.println(b.serialNum);
static int total = 0. WHICH of the following correctly describes its behavior?I. Every object of the class shares the same
total variable.II.
total is reset to 0 each time a new object is created.III.
total can be accessed as ClassName.total from outside the class (if public).1: public class Stats {{
2: private int localVal;
3: private static int sharedVal = 100;
4: public static void printBoth() {{
5: System.out.println(sharedVal);
6: System.out.println(localVal);
7: }}
8: }}
Circuit are created. Circuit has static int power = 10 and int load = 5. After executing:Circuit x = new Circuit();What are
Circuit y = new Circuit();
x.load = 20;
Circuit.power = 50;
y.load and y.power?public class Relay {{
static int baton = 0;
int runner;
Relay(int r) {{ runner = r; baton++; }}
}}
Relay p = new Relay(1);
Relay q = new Relay(2);
System.out.println(p.baton + " " + q.baton + " " + p.runner);
Config with public static String appName = "MyApp". A developer changes it: Config.appName = "NewApp". Then a second object Config c2 = new Config() is created. What is c2.appName?Frequently Asked Questions
Use static for data shared across ALL objects of a class: counters, configuration constants, cached values. Use instance variables for data that is unique per object.
Only if it has an object reference. A static method can call instanceMethod() on an object it receives as a parameter or creates internally, but it cannot call this.instanceMethod() since static has no this.
Yes, it compiles, but it's poor practice and the AP exam will test your understanding that the result is identical to calling it on the class name. The static method does not use the object at all.
Yes. main is static because the JVM calls it before any objects exist. That's why you cannot access instance variables from main unless you first create an object.
Yes. Static variables follow the same default initialization rules: int = 0, double = 0.0, boolean = false, object references = null.
1-on-1 Expert Support
Static vs instance tripping you up? Get targeted help from an AP CSA teacher with a proven track record of doubling student five-rates.
View Tutoring OptionsRelated Topics
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.
tanner@apcsexamprep.com
Courses
AP CSA, CSP, & Cybersecurity
Response Time
Within 24 hours
Prefer email? Reach me directly at tanner@apcsexamprep.com