DEV Community

Cover image for What I learned from Head First: The life of a Java object reference.
Mitchell Mutandah
Mitchell Mutandah

Posted on

What I learned from Head First: The life of a Java object reference.

Hello and welcome! 🤗 As I continue my journey of learning Java fundamentals with Head First Java, I came across an interesting section that I believe could be insightful for others. Sharing it here might spark some curiosity and learning. Let's get started!


Overview

Have you ever considered what it's like to be a Java object reference? Probably not. But today, let's explore how reference variables work. Think of a reference like a remote control. It can control different objects, sometimes isn't set up yet (null), and occasionally leads to objects being removed (garbage collection).

In this piece, I will try to explain Java's object references in a clear way (hopefully 😃). Just useful examples and some moments where things suddenly make sense.

1. What Exactly Is an Object Reference?
In Java, an object reference is not the actual object. It's similar to a remote control that helps you work with an object stored in memory. Here's an example:
- I will refer to this basic class during the entire session

class Dog {
    String name;

    void bark() {
        System.out.println(name + " says woof!");
    }

    public static void main(String[] args) {
        Dog d = new Dog();  // 'd' is a reference to a Dog object
        d.name = "Buddy";
        d.bark(); // Buddy says woof!
    }

}
Enter fullscreen mode Exit fullscreen mode

dog remote

Here, d is the reference. It doesn't contain the Dog object—it just points to where the object exists in memory.

2. Can an Object Reference Change?
Yes! A reference can point to a different object of the same type, like a universal remote that can work with different TVs.

Dog dog1 = new Dog("Rex");
Dog dog2 = new Dog("Luna");

Dog myDog = dog1; // myDog refers to 'Rex'
myDog = dog2; // Now it refers to 'Luna'
Enter fullscreen mode Exit fullscreen mode

reassigning

Here, myDog first controlled dog1 (Rex), then changed to control dog2 (Luna). However, it cannot refer to a Car object. Java strictly checks types.

Car myCar = new Car();
myDog = myCar; // ❌ Compilation Error! A Dog reference can't point to a Car.
Enter fullscreen mode Exit fullscreen mode

3. What Happens When a Reference is final?
When you mark a reference as final, you can't make it point to another object, but the object it points to can still change:

final Dog myDog = new Dog("Rocky");
myDog = new Dog("Bella"); // ❌ Error: Cannot reassign a final reference

myDog.name = "Charlie"; // ✅ Allowed! The object itself can change
Enter fullscreen mode Exit fullscreen mode

final ref

It's like having a remote that can only control one specific TV. You can change the channel (change the object's properties), but you can't use it with a different TV.

4. The Dreaded null Reference
A reference can exist without pointing to any object. That's called null, and it's an empty state:

Dog myDog = null; // myDog exists, but points to nothing
myDog.bark(); // ❌ NullPointerException! myDog doesn't control anything
Enter fullscreen mode Exit fullscreen mode

null ref

This is more like having a remote control without a TV. Press any button—nothing happens.
To avoid this:

  • Always check if a reference is null before using it.
  • Use optional features (like Optional in Java 8+).
  • Set up references correctly.

5. When References Go Away: Garbage Collection

If an object has no references pointing to it, Java's garbage collector frees the memory. This is cutting the last connection to an old device—it gets removed.

Dog dog1 = new Dog();
dog1.name = "Max";
dog1 = null; // Max is now unreachable and eligible for garbage collection
Enter fullscreen mode Exit fullscreen mode

Even more sadly:

Dog dog1 = new Dog();
Dog dog2 = new Dog();

dog1.name = "Bolt"
dog2.name = "Fluffy";

dog1 = dog2; // Now Bolt is lost forever (unless another reference held it)
Enter fullscreen mode Exit fullscreen mode

When dog1 was changed to point to dog2's object, the original Bolt object lost its last reference. Java's garbage collector will eventually clear that memory.

So... In a nutshell...
Being an object reference isn't easy. You get changed, left empty, and sometimes abandoned. But understanding how references work is important for mastering Java memory management.
So next time you write Dog myDog = new Dog("Charlie");, remember—you're not creating a Dog. You're just holding a remote control that points to one.


Let me know if this is something that brings a few lightbulb moments ✨ 💡. Until next time ..... cheers!

cheers

Top comments (0)