Objects, References, and Constructors
AP Computer Science A · Unit 1 — Using Objects and Methods
A variable of a class type holds a reference, not the object
A class is a blueprint; an object is one thing built from that blueprint. The keyword new does three jobs in one step: it reserves memory for a new object, runs a constructor to initialise that object's instance variables, and returns a reference to the object.
Rectangle box = new Rectangle(3, 5);
Read that line as three separate ideas: Rectangle box declares a variable whose type is a class, new Rectangle(3, 5) builds the object, and = stores the reference in the variable. The variable does not contain the rectangle; it contains an arrow that points at the rectangle.
Three consequences appear on the exam every year. First, b = a; between two object variables copies the arrow, so both names then refer to the same object and a change made through one name is visible through the other. Second, a class-type variable that has not been given an object holds null, and calling any method on it throws a NullPointerException at run time, not at compile time. Third, a class may declare several constructors with different parameter lists; the compiler picks the one whose parameter types match the arguments you supply.
int, double, boolean), the memory cell holds the number itself, so b = a creates two independent numbers. With class types, the memory cell holds an address, so b = a creates two arrows pointing to the same place. Every hard object question in Unit 1 comes down to whether you draw this picture correctly: counting the number of rectangles on the right is counting the number of times the word new has run.Worked example: count the new keywords before counting variables
Given a class Counter whose no-argument constructor sets count to 0, whose method increment() increases count by 1, and whose method getCount() returns count. What does the following code print?
Counter a = new Counter(); Counter b = new Counter(); Counter c = a; a.increment(); c.increment(); b.increment(); System.out.println(a.getCount() + " " + b.getCount() + " " + c.getCount());
Solution
Step 1 — count the new keywords. There are exactly two news, so only two objects exist, even though there are three variables.
Step 2 — connect the arrows. a points to the first object, b points to the second object, and c = a copies the arrow so c points to the first object.
Step 3 — accumulate by object, not by variable name. The first object receives two increment commands (one through a, one through c) so its count equals 2. The second object receives one increment command so its count equals 1.
Step 4 — read the result. a.getCount() and c.getCount() ask the same object so both give 2. The line prints 2 1 2.
Someone who answers 1 1 1 counted by variable name rather than by object — that is exactly the trap of the whole chapter.
Common mistake
Assigningc = a and then thinking a copy exists. There is no copy at all: only an object built with new is its own object. How to prevent it: each time you read code, underline every new and draw exactly that many rectangles, and only then connect the variable names to the boxes. Its sibling trap is the NullPointerException: a class-type variable that is declared but not yet assigned a new holds null, and the error blows up only at run time, so the compiler gives no warning at all.Key takeaway
The number of objects equals the number of timesnew runs, not the number of variables. Assignment between two class-type variables copies the arrow, not the object. An unassigned class-type variable holds null, and null.method() blows up at run time.