Creating Objects with the new Keyword
A variable of object type holds a reference to an object, never the object itself, so the object is not 'stored in' the variable.
Why the Box Picture Misleads
Many beginners describe an object variable as a container, a labeled box with an object placed inside it. The phrase sounds reasonable, and many people use it without thinking. In Java, however, that picture is not accurate. The difference matters as soon as a program works with several objects at once, because the box picture predicts behavior that the language does not actually produce.
What do you think happens?
Given a variable std of class type Student, what does std hold once the statement std = new Student(); has finished?
Reveal answer
A reference that leads to the Student object, which lives in the heap
A variable of object type holds a reference, also called a pointer, to the object. The object itself is placed in the heap, a dedicated region of memory, and the variable is only the route the computer follows to reach it. Commit to your guess before reading on, because the step where your reasoning differed is the one the rest of this article corrects.
Watching One Declaration Run
Consider the simplest case: a variable of class type Student is declared, and then a new object is created and assigned to it. The statement below is the one the source uses to illustrate the idea.
- Student std; declares a variable whose type is the class Student
- new
Student()creates an object that is an instance of Student - The assignment records the reference returned by new in the variable std
Read the second line as two separate jobs. One part creates the Student object in the heap. The other part records a reference to that object in std. Once the line has finished, std is not the object. It is the means of reaching the object. When the program later uses std, the computer reads the reference stored there and follows it to the object.
Where Objects and References Live
Objects live in a special region of memory called the heap, and they exist independently of the variables that use them. Think of an object as floating in the heap rather than attached to any one variable. A variable of object type holds a reference, which the source also calls a pointer, to the object. A reference is the address of the memory location where the object is stored. The computer follows that address whenever the program uses the variable.
The diagram shows the correct structure. The variable and the object are two separate things joined by a link. The object is not drawn inside the variable, because the variable does not contain it. The variable is a small holder for a reference, while the object can be much larger, since it sits in the heap rather than inside the variable itself.
A reference is the information a variable of object type holds in order to find its object. It is also called a pointer. The object is the separate thing the reference leads to.
What the new Operator Does, Step by Step
The new operator is the mechanism that brings an object into existence. When you write new followed by a class name and parentheses, the operator does two things. It creates an object that is an instance of that class, and it returns a reference to that object. The returned reference is the value that gets stored. The created object and the reference that points to it are separate things.
The operator also 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, because the new operator invokes it as part of creating the object. Read the sequence as messages in order. The new operator hands control to the constructor, the constructor finishes creating the object, and only then does a reference come back to be stored. The variable receives the reference, not the object.
Once a reference is stored, copying it into another variable copies the reference, not the object. Both variables then refer to the same object. This is the reason the reference model matters: a change made through one variable is visible through the other, because both lead to one object.
Tracing Several Object Variables
Now trace a sequence of statements. Predict, line by line, what each variable refers to before you read the explanation that follows each step. This example is written for this article and extends the single-declaration case from the source.
- Line 1: a is given a reference to a new object, call it object 1
- Line 2: b is given a reference to a second new object, object 2
- Line 3: c receives a copy of b's reference, so c also refers to object 2
- Line 4: b is given a reference to a third new object, object 3; c still refers to object 2
What each variable refers to after every line
After all four lines have run, which object does each of a, b, and c refer to, and is object 1 still reachable through a variable?
- 1After line 1
a refers to object 1. Nothing else has been created yet.
- 2After line 2
b refers to object 2. a still refers to object 1, because line 2 changed only b.
- 3After line 3
c is assigned the value of b, which is a reference. Assigning one object variable to another copies the reference, so c and b both refer to object 2. Neither object has been copied.
- 4After line 4
b receives a reference to a new object, object 3. Reassigning b does not affect c, which still refers to object 2. So b refers to object 3, c refers to object 2, and a refers to object 1.
a refers to object 1, b refers to object 3, and c refers to object 2. Object 1 is still reachable through a, and object 2 is still reachable through c.
Mistakes When Describing Object Variables
Common mistake Saying the object is stored in the variable
What it looks like'The Student object is stored in std.'
Why it failsThe variable does not contain the object. It holds a reference that leads to the object, which lives in the heap.
Do this insteadSay 'std refers to the Student object' or 'std points to the Student object'.
Common mistake Thinking that copying an object variable copies the object
What it looks likeStudent c = b; and then assuming c and b now refer to two separate objects
Why it failsAssigning one object variable to another copies the reference, so both variables refer to the same object.
Do this insteadAfter c = b, both b and c refer to one object. A change made through one is visible through the other.
Common mistake Believing that new returns the object itself
What it looks likeAssuming the value of new Student() is the Student object
Why it failsThe new operator creates the object and returns a reference to it. The reference is what gets stored.
Do this insteadTreat the value of a new expression as a reference that the variable can use to reach the object.
Common mistake Forgetting that the constructor runs inside new
What it looks likeLooking for a separate step to call the constructor before using new
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 insteadWrite new Student() and let the operator call the constructor.
Predict Before You Trace
Given these statements, before reading any further, predict what x refers to after the fourth line, and whether y and z refer to the same object after the third line.
Student x = new Student();
Student y = new Student();
Student z = x;
x = new Student();Hints
- Track each variable separately, and remember that each new creates a new object.
- Assigning one object variable to another copies the reference, not the object.
- Reassigning x changes only x. Ask whether z is affected by that reassignment.
Key Takeaways
- A variable of object type holds a reference, also called a pointer, to an object, and never holds the object itself.
- Objects live in the heap, a dedicated region of memory, and exist independently of the variables that use them.
- The new operator creates an object that is an instance of a class, calls the class's constructor as part of that work, and returns a reference to the new object.
- Assigning one object variable to another copies the reference, so both variables can refer to the same object.
- Describe the relationship with the words 'refers to' or 'points to', not by saying the object is stored in the variable.