Skip to main content

Instance Variables in AP Computer Science A

In AP Computer Science A, instance variables are variables declared inside a class but outside any method. Each object gets its own copy, and together those values define the object's state (a has-a relationship). They are usually private and are initialized by the constructor.

This definition is part of Fiveable's AP Computer Science A review, which also has guided practice questions.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026•Fiveable Content Team

What are the Instance Variables?

An instance variable is a variable declared in the body of a class, outside every constructor and method. Every object you create from that class gets its own separate copy. If you make two Student objects, each one has its own name and its own gpa, and changing one doesn't touch the other.

The CED ties instance variables directly to an object's state. EK 3.4.A.1 says state is an object's attributes and their values at a given time, and those attributes are defined by instance variables. That's the has-a relationship: a Student has a name, has a GPA. Think of the class as a blank form and each object as one filled-out copy. The instance variables are the blanks. The constructor fills them in first (EK 3.4.A.2), methods read and update them later, and the private keyword keeps outside classes from scribbling on the form directly (EK 3.3.A.1).

Why the Instance Variables matter in AP® Computer Science A

Instance variables live in Unit 3: Class Creation, and they show up in almost every topic in that unit. They're the core of AP Comp Sci A 3.4.A (declare instance variables for attributes initialized in constructors). They also anchor AP Comp Sci A 3.3.A (using private for data encapsulation) and AP Comp Sci A 3.8.A (where variables can be used, including how local variables and parameters can hide instance variables). AP Comp Sci A 3.7.A and 3.7.B define class (static) variables and methods by contrast with instance variables, so you can't really understand static without them. Design work under AP Comp Sci A 3.1.A starts with listing a class's attributes, and those attributes become instance variables. Methods in AP Comp Sci A 3.5.A are mostly there to read or change them. On the exam, the class-writing FRQ is basically graded on whether you declare the right private instance variables and use them correctly.

How the Instance Variables connect across the course

Constructors (Unit 3)

Instance variables are the empty slots, and the constructor is what fills them when an object is born. EK 3.4.A.2 says a constructor should give initial values to all instance variables, often by copying in its parameters. If a constructor skips one, that variable keeps its default value (0 for int, false for boolean, null for objects).

Scope, Local Variables, and Shadowing (Unit 3)

Instance variables can be used anywhere in the class. Parameters and local variables only exist inside their own method or constructor (EK 3.8.A.1). When a parameter shares a name with an instance variable, the name refers to the parameter (EK 3.8.A.2). So value = value + 10; in a constructor changes only the parameter, and the instance variable stays untouched. Writing this.value fixes it.

Access Modifiers and Encapsulation (Unit 3)

Declaring instance variables private is how encapsulation actually happens in code (EK 3.3.A.1). Outside classes can't reach in and change the data. They have to go through public methods. That's why accessor (getter) and mutator (setter) methods exist.

Static (Class) Variables and Methods (Unit 3)

An instance variable is one copy per object. A static variable is one copy for the whole class, shared by every object (EK 3.7.B.1). A static method has no specific object to look at, so it can't touch instance variables unless an object is passed in as a parameter (EK 3.7.A.1).

Are the Instance Variables on the AP® Computer Science A exam?

MCQ: Expect code-tracing questions built around instance variables. Common setups:

  • Shadowing traps. A constructor or method declares a local variable or parameter with the same name as an instance variable, and you have to figure out which one actually changed. A classic example is value = value + 10; inside a constructor whose parameter is also named value. The instance variable never gets set, so it stays at its default.
  • Static vs. instance contexts. A static method tries to use an instance variable (a compile error), or a static counter is shared across several objects while each object keeps its own instance fields. You trace what prints.
  • Encapsulation questions. You decide which statements compile when outside code tries to access a private instance variable.

FRQ: The class-design FRQ is where instance variables carry the most points. In 2017 FRQ Q2 you design a class that produces practice problems and implements the StudyPractice interface. In 2018 FRQ Q3 you write a class that implements the StringChecker interface. In 2019 FRQ Q3 the problem involves a class for working with paired delimiters in strings. Each time, you have to:

  1. Declare private instance variables for the object's state.
  2. Initialize all of them in the constructor from its parameters.
  3. Use and update them correctly in methods.

Compare 2017 FRQ Q4, where you write static methods in the Successors class. Static methods have no object state to rely on, so all their data comes through parameters.

The Instance Variables vs Static (Class) Variables

Both are declared in the class outside any method, so they look alike. The difference is ownership. An instance variable belongs to each object, so 30 objects means 30 separate copies. A static variable (marked with static) belongs to the class, so 30 objects share one copy (EK 3.7.B.1). Change a static variable through one object and every object sees the change. Public static variables are accessed with the class name and dot operator, like ClassName.count (EK 3.7.B.2). A quick test is to ask whether the value describes one object (like a name or balance), which makes it an instance variable, or the whole group (like a count of objects created), which makes it static.

Key things to remember about the Instance Variables

  • Instance variables are declared inside a class but outside every method, and each object gets its own separate copy.

  • The values stored in an object's instance variables at any moment make up that object's state, which is a has-a relationship.

  • A constructor should initialize every instance variable, usually by assigning values from its parameters.

  • In AP CSA, instance variables are declared private so outside classes must use public methods to read or change them.

  • If a parameter or local variable has the same name as an instance variable, the name refers to the local one, so use this.name to reach the instance variable.

  • Static methods cannot access instance variables directly because they don't belong to any particular object.

Frequently asked questions about the Instance Variables

What is an instance variable in AP Computer Science A?

It's a variable declared in a class but outside any method, and each object gets its own copy. Its values define the object's state, and the constructor sets its starting values.

Does `value = value;` in a constructor set the instance variable?

No. If the parameter is also named value, both sides refer to the parameter (EK 3.8.A.2), so the instance variable keeps its default, like 0 for an int. Write this.value = value; to assign the instance variable.

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

Each object has its own copy of an instance variable, while all objects share one copy of a static variable (EK 3.7.B.1). Use instance variables for per-object data like a name, and static variables for class-wide data like a running count of objects created.

How are instance variables different from local variables?

Instance variables can be used by any constructor or method in the class and stay alive as long as the object does. Local variables and parameters only exist inside the block where they're declared (EK 3.8.A.1), and they can't be marked public or private.

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

In AP CSA style, yes. Instance variables are declared private to enforce data encapsulation (EK 3.3.A.1). On class-writing FRQs like 2017 Q2 and 2018 Q3, you're expected to declare private instance variables and work with them through methods.