Variables and Reference Types
The new operator creates an object that is an instance of a class and returns a reference to that object.
Committing to a Prediction
Many beginners picture a variable as a labeled box that holds an object. When they write a line that creates a Student, they imagine the Student sitting inside a box named std. This picture feels natural, and it is the first thing this article asks you to set aside. The picture fails in exactly the places where programs behave in surprising ways, so it is worth checking where it goes wrong before relying on any other model.
What do you think happens?
Suppose a variable std has been declared with the class type Student, and the program runs the single statement std = new Student();. What does the variable std hold once that line has finished?
Reveal answer
A reference, also called a pointer, that leads to the Student object
The variable std holds a reference to the object. The object itself lives in the heap, and the variable and the object are two separate things joined by a link. The variable is not a container that the object sits inside.
Watching One Statement Run
Take the single statement from the prediction and watch what it leaves behind. The statement has two parts. On the left, the variable std is declared with the class type Student. On the right, the new operator is applied to the class name followed by parentheses. The result of the right-hand side is what gets stored in std.
- Student is the class, which describes what kind of object is created
- std is the variable name that will hold the reference
- new
Student()creates a Student object and returns a reference to it - The = stores the returned reference in std, not the object
Tracing std after one creation statement
After the statement Student std = new Student(); finishes, what does the variable std contain, and where does the Student object live?
- 1Evaluate the right-hand side
The new operator runs. It creates an object that is an instance of the Student class.
- 2Run the constructor
As part of creating the object, the new operator calls the constructor defined in the Student class, which sets up the new instance.
- 3Receive the reference
The new operator returns a reference to the created object. That returned reference is the value produced by the right-hand side.
- 4Store the reference
The assignment stores that reference in std. The variable std now refers to the object, and the object lives in the heap.
std holds a reference to the Student object. The object itself is in the heap, not inside std.
The arrow in the diagram carries the important meaning. It shows that std refers to the object. The object is not drawn inside the variable box, because the variable does not contain it. If you catch yourself saying that the object is stored in std, replace that sentence with one saying that std refers to the object.
What the new Operator Does
The new operator is the mechanism that brings an object into existence in a program. When new is followed by a class name and parentheses, the operator creates an object that is an instance of that class, and it returns a reference to that object. The returned reference is what gets stored. The created object is a separate thing from the reference that points to it.
The operator does more work behind the scenes. It calls a special subroutine defined in the class, called a constructor. The constructor is the part of the class responsible for setting up a new instance. You do not call the constructor as a separate step; the new operator invokes it as part of creating the object.
Read the diagram as a sequence of messages rather than as a single action. The new operator first hands control to the constructor, the constructor finishes creating the object, and only then does a reference come back to be stored in the variable. The variable is the last participant in the chain, and it receives a reference, not the object.
Variable Versus Object
The two things in this topic are easy to blur together, so keep them separate. The variable is the name, such as std. The value the variable holds is a reference, which is also called a pointer. The object is the instance of the class that exists in the heap. The reference is the link; the object is the thing the link leads to.
Tracing a Sequence of Creations
Now apply the same model to several statements in a row. Each new statement creates its own object and returns its own reference. The variables do not share a box; each one simply refers to whatever reference was most recently assigned to it. Trace the sequence below one line at a time.
- Line 1 creates the first Student object and stores its reference in a
- Line 2 creates a second, separate Student object and stores its reference in b
- After both lines, two Student objects exist, and each variable refers to one of them
Predicting references after two creations
Starting from an empty program, the statements Student a = new Student(); and then Student b = new Student(); run in order. How many objects exist, and what does each variable refer to?
- 1After line 1
The first new operator creates one Student object and returns a reference to it. a stores that reference, so a refers to object 1. One object exists.
- 2After line 2
The second new operator creates a separate Student object and returns a new reference. b stores that reference, so b refers to object 2. The reference in a is unaffected by this line.
- 3Count the objects
Each new statement created its own object, so two objects exist in the heap. Creating an object is not the same as assigning a variable; the variable only holds the link.
a refers to the first Student object, b refers to the second, and there are two objects in total.
Mistakes Beginners Make
Common mistake Saying the object is stored inside the variable
What it looks likeStudent std = new Student(); read as 'the Student is sitting in the box named std'
Why it failsThe variable does not contain the object. The object lives in the heap, and the variable holds a reference that leads to it.
Do this insteadSay that std refers to, or points to, the Student object.
Common mistake Thinking the variable receives the object instead of a reference
What it looks likeExpecting std to hold the Student itself immediately after the assignment
Why it failsThe new operator returns a reference to the object it created, and that returned reference is what gets stored.
Do this insteadAfter the assignment, the value in std is the reference returned by new.
Common mistake Forgetting that the constructor runs as part of new
What it looks likeBelieving the object exists before new is evaluated, with the constructor called separately afterward
Why it failsThe new operator invokes the constructor as part of creating the object. You do not call it as a separate step.
Do this insteadRead new as one operation that creates the object, runs the constructor, and returns a reference.
Practice Tracing References
Given the statements Student x = new Student(); and then Student y = new Student();, state for each variable whether it refers to an object, and how many Student objects exist after both lines have finished. Write your answer before checking the hints.
Hints
- Ask what the new operator returns on each line, not what the variable contains as a box.
- Each new statement creates a separate object, so count how many new statements ran.
Key Takeaways
- The new operator creates an object that is an instance of a class, calls the class's constructor as part of that creation, and returns a reference to the object.
- A variable such as std holds a reference, also called a pointer, to the object; the object itself lives in the heap.
- Describe the relationship as std refers to the object, not as the object being stored in std.
- Each new statement creates its own object and returns its own reference, so tracing a sequence means tracking which reference each variable most recently received.