Null References and Missing Objects
A variable of object type holds a reference to an object, never the object itself, so the object is not 'stored in' the variable.
The Container Belief
When beginners first meet object variables, they often describe them in everyday language: a variable is a container, and an object is put inside it. The phrase sounds reasonable, and many people use it without thinking. In Java, however, that picture is not accurate, and the difference matters once you start writing programs that work with several objects at once.
This article starts from the wrong belief, tests it with a prediction, and then builds the correct model from the ground up. By the end you should be able to say exactly what a variable holds and exactly where the object is.
What do you think happens?
Consider a single declaration in which a new Student object is created and assigned to a variable called std. At the moment the statement finishes, where is the Student object, and what is inside the variable std?
Reveal answer
The Student object lives in the heap, a region of memory separate from the variable. The variable std does not contain the object; it holds a reference, which is the information needed to find the object.
Many learners predict that the whole object sits inside std, the way a coin sits inside a purse. The source's model is different: objects float independently in the heap, and variables hold a route to them.
Testing the Container Belief
The belief to examine is this: an object is stored in a variable, so the variable holds the object. Learners who hold this belief tend to imagine the variable as a small compartment with the whole object sitting inside it. Before reading the explanation, you were asked to commit to an answer about where the Student object is. If you pictured the object inside std, the container belief is doing the predicting, and the next sections test whether it survives scrutiny.
Where Objects Live in Memory
Think of objects as floating around independently in the computer's memory rather than being attached to the variables that use them. A dedicated region of memory, called the heap, is where objects live. The objects themselves are placed there, and the variables that need them are separate from them.
The diagram separates two things that the container belief merges. The variable std is in one place, and the object is in the heap. The only link between them is the reference, which the variable carries. The object is not inside the variable at all.
What a Reference Holds
A reference, also called a pointer, is the address of the memory location where an object is stored. An object variable holds a reference to an object, never the object itself.
So what does a variable hold? Instead of the object, it holds the information needed to find the object. The variable carries a way of getting to the object, not the object's contents. When the program later uses std, the computer reads the reference stored in std and follows it to the object. The variable is never the object. It is the route the computer uses to reach the object.
Tracing the std Declaration
Reading one declaration as two jobs
A statement creates a new Student object and stores a reference to it in the variable std. Trace what happens in memory and what std holds afterward.
- 1Split the statement
The statement does two separate jobs. One part creates the object in the heap. The other part records the reference in std.
- 2Create the object
A new instance of the class Student is placed in the heap. It floats there independently of any variable.
- 3Record the reference
The variable std stores the reference that leads to that object. std does not store the object's contents.
- 4Follow it later
When the program uses std, the computer reads the reference in std and follows it to the Student object in the heap.
After the statement finishes, the Student object is in the heap, and std refers to it. The object is not stored in std.
- Student std is the variable declaration: std is a variable of object type Student
- new
Student()creates the object in the heap - The = records the reference in std that refers to the new object
Choosing Precise Language
The source is careful about language, and it is worth adopting the same habit. Saying the object is the value of the variable std is not quite true, and saying the object is stored in the variable std is simply false. The more accurate statement is that the variable std refers to or points to the object.
| Loose phrasing | Precise phrasing |
|---|---|
| std is an object | std is a variable that refers to a Student object |
| The object is stored in std | std refers to the object in the heap |
| std holds the Student | std holds a reference to the Student |
| std points to the Student | std points to the Student object in the heap |
Shorthand is common in conversation, but read each loose phrase as meaning the precise one.
Shorthand such as std is an object hides the real structure. Read such a phrase as meaning that std is a variable that refers to an object. Keeping this reading in mind prevents the misconception from creeping back in as you write more code.
Two Variables, One Object
Because a variable holds a reference rather than the object, assigning one object variable to another copies the reference. Both variables then refer to the same object. Nothing about the object is duplicated.
After the second line, first and second both refer to the same Student object in the heap. The reference was copied; the object was not. A change made to that object through either variable is visible through the other, because there is only one object.
Mistakes With Object Variables
Common mistake Believing the object sits inside the variable
What it looks likeThinking that after Student std = new Student(), the whole Student object is contained within std
Why it failsThe variable holds only a reference. The object lives in the heap, separate from the variable.
Do this insteadSay that std refers to a Student object in the heap
Common mistake Thinking assignment duplicates the object
What it looks likeAssuming Student second = first creates a second, separate Student
Why it failsAssigning one object variable to another copies the reference, so both variables refer to the same object.
Do this insteadSay that second now refers to the same object that first refers to
Common mistake Saying a variable is the object
What it looks likeWriting std is the Student when describing the program
Why it failsThe variable is the route the computer uses to reach the object, not the object itself.
Do this insteadUse refers to or points to when naming the connection
Null: A Reference That Refers to Nothing
The reference model also explains missing objects. A variable of object type holds a reference, and a reference is a way of reaching an object. When an object variable refers to no object, it is said to hold a null reference. The variable still exists and still has a type, but there is no object in the heap for it to lead to.
Practice Tracing References
Given the statements Student a = new Student(); and then Student b = a;, describe in your own words where the Student object lives, what a holds, and what b holds. Use the terms refers to or points to in your answer.
Hints
- Ask whether the object itself is copied or only the reference
- Count how many Student objects the two statements create
- Check whether your sentence describes a variable containing an object
Summary of the Reference Model
- 1An object variable holds a reference to an object, never the object itself
- 2Objects live in the heap and float independently of the variables that use them
- 3A reference is the route the computer follows from the variable to the object; say that a variable refers to or points to its object
- 4Assigning one object variable to another copies the reference, so both variables can refer to the same object
At a glance
- An object variable holds a reference to an object, never the object itself, so the object is not stored in the variable
- Objects live in the heap, a region of memory separate from the variables that use them
- Use precise terms: a variable refers to or points to an object
- Assigning one object variable to another copies the reference, so both can refer to the same object