Reference Variable

In AP Computer Science A, a reference variable is a variable that stores the memory address of an object rather than the object itself, letting you access that object's methods and instance variables; it's how all non-primitive (reference) types work in Java.

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

What is Reference Variable?

A reference variable doesn't hold an object. It holds the memory address where the object lives, like a sticky note with a house address written on it instead of the house itself. When you write Dog d = new Dog();, the new keyword builds the actual Dog object in memory, and d just stores directions to it. You then use d with the dot operator to call methods and access the object's data.

This one idea explains a lot of Java behavior that trips people up. Two reference variables can point to the same object, so changing the object through one variable changes what the other one sees. A reference variable can also hold null, which means it points to nothing at all, and calling a method on it throws a NullPointerException. Every variable in Java is either a primitive type (int, double, boolean) or a reference type, and reference variables are how the reference side works.

Why Reference Variable matters in AP Computer Science A

Reference variables show up the moment you start using objects in AP CSA and never leave. They're behind how new and constructors work, why null exists, how objects get passed to methods (the method receives a copy of the reference, so it can modify the original object), and why == compares addresses while .equals() compares contents. Most importantly, they power polymorphism in the inheritance unit. A superclass reference variable can point to a subclass object, like Shape s = new Rectangle();, and the exam tests this constantly. If you don't have a solid mental model of "the variable is just an address tag," inheritance questions will feel like magic instead of logic.

How Reference Variable connects across the course

Object and the New Keyword (Unit 2)

The new keyword creates the object in memory and hands back its address, and the reference variable is where that address gets stored. Declaration makes the sticky note; new builds the house and writes the address on it.

null and Null Reference (Unit 2)

A reference variable set to null points to no object at all. Calling a method through a null reference causes a NullPointerException, one of the most-tested runtime errors in MCQs.

Polymorphism and Inheritance (Unit 9)

Because the variable only stores an address, a superclass reference like Animal a can legally point to a Dog object. The variable's declared type controls which methods you're allowed to call; the object's actual type controls which version runs.

Garbage Collection (Unit 2)

When no reference variable points to an object anymore, Java's garbage collector reclaims that memory automatically. Reassigning a reference can leave its old object unreachable, which is exactly when garbage collection kicks in.

Is Reference Variable on the AP Computer Science A exam?

Reference variables get tested in two main ways. In Units 1-2-style MCQs, you'll trace code where two references point to the same object, or where a null reference causes a NullPointerException, or where == returns false for two objects with identical contents. In inheritance questions, the exam loves declarations like Shape s = new Rectangle(); and asks which assignments compile. The rule to apply is that a superclass reference can hold a subclass object, but not the reverse, and you can only call methods declared in the reference variable's type. On FRQs, no released question uses the phrase "reference variable" verbatim, but every FRQ that has you write or use a class depends on you handling references correctly, especially when a method parameter is an object and your changes need to (or must not) affect the original.

Reference Variable vs Primitive Variable

A primitive variable (int, double, boolean) stores the actual value directly, so copying it makes a truly independent copy. A reference variable stores an address, so copying it gives you a second variable pointing at the same object. That's why int x = y; then changing y doesn't touch x, but two references to the same object always see each other's changes. It's also why primitives can never be null but references can.

Key things to remember about Reference Variable

  • A reference variable stores the memory address of an object, not the object itself, and you use it with the dot operator to call the object's methods.

  • Assigning one reference variable to another copies the address, so both variables point to the same object and changes through either one are visible through both.

  • A reference variable holding null points to no object, and calling any method on it throws a NullPointerException.

  • A superclass reference variable can hold a subclass object (Shape s = new Rectangle();), but a subclass reference cannot hold a superclass object.

  • The == operator compares whether two reference variables hold the same address, while .equals() compares the contents of the objects.

  • When an object has no reference variables pointing to it, Java's garbage collector automatically frees its memory.

Frequently asked questions about Reference Variable

What is a reference variable in AP Computer Science A?

It's a variable that stores the memory address of an object instead of the object itself. In Dog d = new Dog();, the object lives in memory and d just holds directions to it, which you follow using the dot operator.

Does a reference variable actually contain the object?

No. It only contains the object's address. That's why two reference variables can point to the same single object, and why setting a reference to null doesn't delete the object, it just disconnects that variable from it.

What's the difference between a reference variable and a primitive variable?

A primitive variable like an int stores its value directly, so copies are independent. A reference variable stores an address, so copying it creates a second pointer to the same object. Primitives also can't be null, but references can.

Can a superclass reference variable hold a subclass object?

Yes, and this is heavily tested. Animal a = new Dog(); compiles fine because a Dog is-an Animal. The reverse, Dog d = new Animal();, does not compile. The reference type also limits which methods you can call without casting.

Why does == return false for two objects that look identical?

Because == compares the addresses stored in the reference variables, not the objects' contents. Two separately constructed objects live at different addresses even with the same data, so use .equals() to compare what's inside them.