Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 2

References and aliasing

Why two variables can secretly be one object

16m45 XP
→→→

What you'll be able to do

  • Explain what a reference variable actually stores
  • Predict the result when two variables point at one object
  • Say what happens to an object passed into a method
  • Use == and .equals() for the right question
1

Object References

A variable holds an address, not the object

This single idea explains half of the confusing bugs beginners hit — and it is a favourite interview question.

Two remotes, one television

Give a friend a second remote control for your TV. Now there are two remotes but still one television: whoever presses volume-up, *your* TV gets louder. Reference variables are remotes; the object is the TV. Tv b = a; hands out a second remote — it does not buy a second TV.

Aliasing, line by line

Main.java
1Tv a = new Tv();
2Tv b = a;
3b.turnUp();
4System.out.println(a.volume);
Tv a = new Tv();
One object is created; a stores its address.
Tv b = a;
The address is copied, not the object. Both variables now point at the same heap object — they are aliases.
b.turnUp();
Follows the address and changes the shared object's field.
System.out.println(a.volume);
Reads the same object, so it shows the change made through b. There is only one volume in existence.

After `Tv b = a;` and two turnUp() calls

Stack
a→ref → #1
b→ref → #1
Heap
#1: Tv
volume2

Two stack slots, one heap object. Nothing was duplicated.

Passing an object into a method

Java always passes by value — but for objects the value being copied is the reference. So the method receives its own remote pointing at your object: changes to the object's *fields* are visible to you, while reassigning the parameter (box = new Box();) only repoints the method's local remote and changes nothing outside.

== vs .equals() — pick the right question

a == b asks *same object?* — useful for identity checks and for x == null. a.equals(b) asks *equivalent content?* — and it only behaves sensibly if the class defines what equality means. For your own classes without an equals method, both end up comparing identity.

Prove the aliasing yourself

Predict all three lines first. Then add `b = new Tv();` before the prints and see which lines change.

Output
Press Run to execute this example.

Prove it — coding challenges

Common mistakes

WrongTv b = a; // now I have two TVs
WhyOnly the reference was copied; both variables share one object.
FixTv b = new Tv(); — `new` is the only thing that creates an object.
Wrongif (student1 == student2) { /* same name? */ }
Why`==` compares addresses, so two students with identical data are still 'not equal'.
FixCompare the fields you care about: `student1.name.equals(student2.name)`.
Wrongstatic void replace(Box box) { box = new Box(); }
WhyReassigning the parameter repoints only the local copy of the reference.
FixMutate through the reference (`box.value = 5;`) or return the new object.

Lesson playground

Free practice

Two remotes, one TV. Change `b = a;` into `b = new Tv();` and watch every printed line change meaning.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

One method mutates the object, the other reassigns the parameter. What is printed?

Main.java
1class Box {
2 int value;
3}
4
5public class Main {
6 static void modify(Box box) {
7 box.value = 99;
8 }
9
10 static void replace(Box box) {
11 box = new Box();
12 box.value = 5;
13 }
14
15 public static void main(String[] args) {
16 Box b = new Box();
17
18 modify(b);
19 System.out.println(b.value);
20
21 replace(b);
22 System.out.println(b.value);
23 }
24}

Summary

  • A reference variable stores an address; the object lives on the heap.
  • Assigning one reference to another creates an alias — one object, two names.
  • Methods receive a copy of the reference: field changes are visible outside, reassignment is not.
  • `==` asks 'same object?'; comparing content means comparing fields.