🏗️ Classes & Objects · Intermediate

References, aliasing & null in Java

Two variables can point to one object; NullPointerException and helpful NPE messages.

🧩 The mysteryChange an object through a friend's variable and yours changes too. But reassign it inside a method and nothing happens. Same Java, two behaviors — one picture explains both.

Variables hold addresses

An object is a house; a variable is a slip of paper with its address. Box b = a; copies the address, not the house. Now two slips lead to one house — that's called *aliasing*.

Box a = new Box();
Box b = a;   // same house, two slips
🔮 Predict it

Paint through the alias

What does this print?

class Box { int v; }
void main() {
    Box a = new Box();
    Box b = a;
    b.v = 7;
    System.out.println(a.v);
}
  1. 0
  2. 7
  3. null
Show the answer

7. a and b are aliases for the same Box, so a change made through b is visible through a.

Passing to methods

Java passes a copy of the reference (pass by value). The method gets its own slip with the same address: it can change the house, but if it writes a new address on its slip, the caller's slip is unaffected.

🔮 Predict it

Two moves in one method

What does this print?

class Box { int v; }
void paint(Box b) {
    b.v = 1;
    b = new Box();
    b.v = 2;
}
void main() {
    Box a = new Box(); paint(a);
    System.out.println(a.v);
}
  1. 0
  2. 1
  3. 2
Show the answer

1. The first line changed the shared object. Then b was pointed at a brand-new Box — the caller's a never saw that, so the 2 went to an object nobody keeps.

null: a blank slip

A reference can be **null** — a slip with no address. Reference-type fields start as null. Using a null reference (calling a method, reading a field) throws **NullPointerException**.

⚠️ The trap

The helpful NPE

name was never assigned, so it's null, and .length() crashes. Since Java 14 (on by default from Java 15, JEP 358), the message names the culprit: *Cannot invoke "String.length()" because "u.name" is null*.

class User { String name; }
User u = new User();
u.name.length();  // NPE
💼 In the real world

In real projects

Aliasing bugs are sneaky: two parts of an app share one list, and one "harmlessly" clears it. NullPointerException is the most common Java exception in production — helpful NPE messages turned many hour-long hunts into a glance at the log.

Key takeaways

  1. Assignment copies the reference, not the object
  2. Aliases see each other's changes
  3. Java passes references by value to methods
  4. Helpful NPE messages name the null expression

💡 A reference is a house address on a note. Copy the note and both notes lead to the same house.

🤯 Did you know?

On 64-bit JVMs, references are often squeezed into 32 bits ("compressed oops"), turned on by default for heaps under about 32 GB — saving lots of memory on every reference.

Practice questions

What does this print?

class Box { int v; }
void main() {
    Box a = new Box();
    Box b = a;
    b.v = 9;
    System.out.println(a.v);
}
  1. 0
  2. 9
  3. null
  4. Compile error
Check your answer

9. a and b are aliases for the same Box, so changing the object through b is visible through a.

What does this print?

class Box { int v; }
void reset(Box b) {
    b = new Box(); b.v = 1;
}
void main() {
    Box a = new Box();
    a.v = 5;
    reset(a);
    System.out.println(a.v);
}
  1. 1
  2. 5
  3. 0
  4. null
Check your answer

5. Java passes a copy of the reference. reset points its own copy at a new Box; the caller's a still points to the original, untouched object.

Next: you never delete objects in Java. So where do they all go?