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.

Instance Variables

  • Declared without the static keyword inside a class but outside any method
  • Each object gets its own separate copy
  • Accessed via this.field or just field inside instance methods
  • Destroyed when the object is garbage-collected

Static Variables and Methods

  • Declared with the static keyword
  • 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

After creating 3 Tickets, what is each object's number?
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

Does this compile? What does it print if you fix the error?
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

Which variables are shared after two objects are created?
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

1. Accessing Instance Variables from Static Methods

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.

2. Assuming Static Variables Reset Per Object

Static variables do NOT reset when new objects are created. A static counter continues accumulating across all constructor calls.

3. Confusing Which Copy Is Modified

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.

4. Calling Static Methods via Object References

obj.staticMethod() compiles but is considered poor style. Always use ClassName.staticMethod() to make the static nature clear.

AP Exam Tip: When you see a class with a static variable and the question asks what each object's value is, trace through every constructor call in order. The static variable accumulates across all of them.
⚠ Watch Out: A static method accessing an instance variable is a compile error, not a runtime error. The MCQ will ask you to identify which line fails to compile—look for a static method body that references a non-static field without an object reference.

Check for Understanding

Score: 0 / 0 answered (8 total)
1. After executing the code below, what does the last println print? PREDICT before reading.
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);
2. A class has 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).
3. Spot the error. WHICH line causes a compile error?
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: }}
4. Two objects of class Circuit are created. Circuit has static int power = 10 and int load = 5. After executing:
Circuit x = new Circuit();
Circuit y = new Circuit();
x.load = 20;
Circuit.power = 50;
What are y.load and y.power?
5. WHICH statement about static methods in AP CSA is ALWAYS true?
6. A student writes a static method that should count vowels in a String. WHICH header is correctly declared?
7. Code trace: PREDICT the output.
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);
8. Consider a class 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 Options

Related 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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com