Instance Variables

In AP Computer Science A, instance variables are variables declared inside a class but outside any method, usually marked private, that store the state of each individual object. Every object gets its own copy, so two Rectangle objects can have different lengths and widths.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What are Instance Variables?

Instance variables are the data a class remembers. You declare them inside the class but outside every method and constructor, and each object built from that class gets its own personal copy. That's what "instance" means here. If you create two Rectangle objects, each one carries its own length and width values, completely independent of the other.

In AP CSA, instance variables are almost always declared private. That's not just style, it's the principle of encapsulation. Outside code shouldn't reach in and grab an object's data directly. Instead, the class provides public methods (accessors to read, mutators to change) that act as the official doorway. The constructor's whole job is to give these variables their starting values when the object is created. Together, instance variables define an object's state, while methods define its behavior.

Why Instance Variables matter in AP Computer Science A

Instance variables live at the heart of Unit 5 (Writing Classes), where you learn the anatomy of a class, and they're the payoff of Unit 2 (Using Objects), where you first see that objects carry their own data. The whole object-oriented model on this exam rests on one idea. A class is a blueprint, an object is a thing built from that blueprint, and instance variables are what make each object distinct. Understanding when data belongs to the object (instance variable), to a single method call (local variable), or to the whole class (static variable) is one of the most-tested distinctions in the course. It also feeds directly into encapsulation, mutability, and aliasing questions later on.

How Instance Variables connect across the course

Constructor (Unit 5)

Constructors exist to initialize instance variables. The classic pattern, like length = l; inside Rectangle(int l, int w), copies parameter values into the object's instance variables so the object starts in a valid state.

Access Modifiers (Unit 5)

On the AP exam, instance variables should be declared private. Public methods then control access. Marking them public on the class design FRQ costs you a point, so private plus getters and setters is the safe pattern.

Mutable Object (Units 2 & 5)

An object is mutable precisely because its instance variables can change after construction. Mutator methods like scale(double factor) work by reassigning instance variables, which is why two references to the same object both see the change.

Methods (Units 2 & 5)

Instance variables are the state, methods are the behavior that reads or updates that state. Unlike local variables, instance variables survive between method calls, which is how an object "remembers" things from one call to the next.

Are Instance Variables on the AP Computer Science A exam?

Instance variables show up in both sections. In multiple choice, you'll trace code and track each object's state, decide what a mutator method does to an object (Fiveable practice questions on mutators are really instance-variable questions in disguise), or spot scope errors where code treats a local variable like an instance variable. In the free response section, the class design question is where instance variables earn or lose real points. FRQs like 2017 Q2 (StudyPractice) and 2018 Q3 (StringChecker) ask you to write a complete class, and the rubric expects appropriate private instance variables, a constructor that initializes them, and methods that use them correctly. Declaring them public, redeclaring them inside the constructor (which creates local variables that shadow them), or forgetting to initialize them are the classic point-losers.

Instance Variables vs Local Variables

Instance variables are declared in the class and belong to the object; they exist as long as the object exists and keep their values between method calls. Local variables are declared inside a method or constructor and vanish when that method finishes. The sneakiest trap is shadowing. Writing int length = l; inside a constructor creates a brand-new local variable instead of setting the instance variable, leaving the real length at its default value of 0.

Key things to remember about Instance Variables

  • Instance variables are declared inside the class but outside all methods, and every object gets its own independent copy.

  • On the AP exam, instance variables should always be declared private, with public accessor and mutator methods controlling access (encapsulation).

  • Constructors initialize instance variables; re-declaring the variable's type inside the constructor creates a shadowing local variable and is a common FRQ point-loser.

  • Instance variables keep their values between method calls, which is what lets an object remember its state, unlike local variables that disappear when a method ends.

  • If instance variables aren't explicitly initialized, they get default values (0 for int, 0.0 for double, false for boolean, null for object references).

  • The class design FRQ almost always awards points for correctly declared private instance variables, a constructor that sets them, and methods that use them.

Frequently asked questions about Instance Variables

What is an instance variable in AP Computer Science A?

An instance variable is a variable declared inside a class but outside any method that stores the state of each individual object. Each object created from the class gets its own copy, so two objects can hold different values for the same variable.

What's the difference between an instance variable and a local variable?

Instance variables belong to the object, are declared at the class level, and persist as long as the object exists. Local variables are declared inside a method or constructor and are destroyed when it finishes. Only instance variables get automatic default values like 0 or null.

Do instance variables have to be private on the AP exam?

Yes, treat it as a rule. The class design FRQ rubric expects private instance variables, and declaring them public can cost you a point. Outside code should interact with them only through public methods.

Are instance variables the same as fields or attributes?

Essentially yes. "Field" and "attribute" are other names for the same idea, but the AP CSA exam and the College Board consistently say "instance variable," so use that term on the FRQ.

What happens if I don't initialize an instance variable?

Java assigns a default value automatically. Numeric types get 0 or 0.0, booleans get false, and object references like String get null. This matters because a forgotten initialization in a constructor can leave a reference null and cause a NullPointerException later.