Concepts / Creating Objects with the new Keyword

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.

  • Programming

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?

  • The Student object itself, stored inside std

  • A reference that leads to the Student object, which lives in the heap

Reveal answer
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.

java
Notes on the code
  • 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.

stdvariableStudent objectlives in the heapreferencethe route to the objectholdspoints to
What is actually stored in the variable std, and how does the arrow from the variable reach the separate object it refers to?

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.

Definition

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.

new operatorStudent constructorStudent object inheapstd1. calls constructor2. sets up new instance3. returns reference, stored in std
What steps happen in order when new is evaluated, and which of those steps produces the value that gets assigned to the variable?

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.

java
Notes on the code
  • 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
Worked example

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?

  1. 1
    After line 1

    a refers to object 1. Nothing else has been created yet.

  2. 2
    After line 2

    b refers to object 2. a still refers to object 1, because line 2 changed only b.

  3. 3
    After 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.

  4. 4
    After 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.

Result

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.

brefers to object 2object 2in the heapbrefers to object 3object 2in the heapcrefers to object 2crefers to object 2object 3in the heaprefers torefers torefers torefers to
After line 4 runs, which object does each variable refer to, and does c change when b is reassigned?

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 fails

    The variable does not contain the object. It holds a reference that leads to the object, which lives in the heap.

    Do this instead

    Say '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 like

    Student c = b; and then assuming c and b now refer to two separate objects

    Why it fails

    Assigning one object variable to another copies the reference, so both variables refer to the same object.

    Do this instead

    After 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 like

    Assuming the value of new Student() is the Student object

    Why it fails

    The new operator creates the object and returns a reference to it. The reference is what gets stored.

    Do this instead

    Treat 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 like

    Looking for a separate step to call the constructor before using new

    Why it fails

    The new operator invokes the constructor as part of creating the object. You do not call it as a separate step.

    Do this instead

    Write new Student() and let the operator call the constructor.

Predict Before You Trace

MEDIUM

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.