Private instance variable

A private instance variable is a field declared with the private access modifier inside a class, so only code within that class can read or change it directly; other classes must go through public methods like getters and setters, which is how Java enforces encapsulation on the AP CSA exam.

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

What is Private instance variable?

A private instance variable is a piece of data that belongs to an object and is marked with the private keyword, like private double balance; inside a BankAccount class. "Instance" means every object gets its own copy (your account's balance, my account's balance). "Private" means only code written inside that same class can touch it directly. If a client class tries myAccount.balance, the code won't even compile.

So how does anyone use the data? Through the class's public methods. A getter (accessor) like getBalance() returns the value, and a setter (mutator) like deposit() changes it in a controlled way. Think of it like an ATM. You never reach into the bank vault yourself. You make requests through a machine that follows the bank's rules. That gap between the data (hidden) and the interface (public) is the whole point of encapsulation, and it's a convention AP CSA treats as non-negotiable: instance variables should always be private.

Why Private instance variable matters in AP Computer Science A

Private instance variables live at the center of Unit 5 (Writing Classes), where you learn to design a class from scratch by choosing its data and its public interface. The CED's expectation is blunt: when you write a class, you declare instance variables as private and provide access only through methods. This idea also echoes backward into Unit 2 (Using Objects), where you call getters precisely because you can't reach the data directly, and forward into Unit 9 (Inheritance), where even a subclass cannot directly access its parent's private variables and must use inherited public or protected methods instead. If you understand why private exists, you understand data hiding, encapsulation, and why every released class design FRQ solution starts with private fields.

How Private instance variable connects across the course

Encapsulation (Unit 5)

Encapsulation is the design principle; private instance variables are how you actually do it in Java. Marking data private and exposing it only through public methods means the class controls its own state instead of trusting every other class to behave.

Public instance variable (Unit 5)

The anti-pattern. A public instance variable lets any class reach in and overwrite the data, which breaks data hiding. On the class design FRQ, declaring instance variables public instead of private costs you a rubric point.

Data hiding (Unit 5)

Data hiding is the payoff of going private. The class can change how it stores information internally without breaking client code, because clients only ever talked to the public methods anyway.

Inheritance and private access (Unit 9)

A common surprise: a subclass inherits its parent's private instance variables but cannot access them directly. The subclass has to call the parent's public getters or use super's constructor, which is a favorite MCQ trap.

Is Private instance variable on the AP Computer Science A exam?

This shows up two ways. In MCQs, you'll see code where a client class tries to access something like account.balance directly, and you have to recognize that it won't compile because balance is private. Questions ask things like "which code segment lets a client class access the private instance variable balance?" and the answer always routes through a public accessor method. Other MCQs test the purpose of getters and setters, which only exist because the underlying data is private. On the free response, the class design question (historically FRQ 2, like StepTracker in 2019) asks you to write a complete class. The scoring guidelines award a point for declaring appropriate private instance variables, so writing private int totalSteps; instead of int totalSteps; is literally worth points. Make private your default for every instance variable you write on the exam.

Private instance variable vs Public instance variable

Both are fields that belong to an object; the difference is who can touch them. A public instance variable can be read and modified by any class (account.balance = -5000; would compile), while a private one can only be accessed inside its own class. AP CSA expects private. Public instance variables technically work in Java, but they destroy encapsulation, and on the FRQ they cost you the instance variable declaration point.

Key things to remember about Private instance variable

  • A private instance variable can only be accessed by code inside its own class; any other class trying to use it directly causes a compile-time error.

  • Other classes interact with private data through public accessor (getter) and mutator (setter) methods, which is what encapsulation looks like in actual Java code.

  • On the AP CSA class design FRQ, declaring your instance variables as private is part of the scoring rubric, so always write the private keyword.

  • Each object gets its own copy of every instance variable, so two BankAccount objects have two separate private balance values.

  • A subclass inherits its parent's private instance variables but cannot access them directly; it has to use the parent's public methods or constructor.

Frequently asked questions about Private instance variable

What is a private instance variable in AP Computer Science A?

It's a field inside a class declared with the private access modifier, like private double width;. Only code within that same class can read or modify it directly, and other classes must use public methods like getters and setters.

Can a subclass access a private instance variable from its parent class?

No. The subclass inherits the variable (the data exists in the object), but it cannot reference it directly by name. It has to use the parent's public getter methods or pass values up through super() in the constructor. This is a classic Unit 9 MCQ trap.

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

A private instance variable is declared in the class body, belongs to the object, and lives as long as the object does. A local variable is declared inside a method, has no access modifier, and disappears when the method finishes running.

Do I lose points on the AP CSA FRQ if my instance variables aren't private?

Yes, on the class design free-response question (like StepTracker on the 2019 exam), the rubric awards a point for declaring appropriate private instance variables. Leaving off the private keyword or making them public costs you that point.

If a variable is private, how do other classes get its value?

Through public accessor methods. For example, a BankAccount class with private double balance; provides a method like public double getBalance() { return balance; } so client classes can read the value without being able to change it directly.