# Java References Made Easy: Explore the Box Analogy

## Intro

If you want to fully understand **Java classes, objects, and linked list references**, this blog is for you.  
I used to be confused about when to use `new` and how references work. Let’s clear it using the **box analogy**.

## The `new` keyword

```java
Node n = new Node(10);
```

* **Right side (**`new Node(10)`) → creates a **new box (object)** in memory with `data=10` and `next=null`
    
* **Left side (**`n`) → a **reference variable** storing the box’s **address**
    

**Analogy:**  
`n` holds the address of a box with `10` inside.

```plaintext
[ data=10 | next=null ]  <-- box
       ^
       |
       n
```

---

## Reference without `new`

```java
Node temp = head;
```

* No new box is created.
    
* `temp` just copies the **address** that `head` has.
    
* Both `head` and `temp` point to the **same object**.
    

```plaintext
[ data=5 | next=null ]  <-- box
       ^       ^
       |       |
      head    temp
```

---

## Linked List Example

```java
Node head = new Node(5);      // new box1
Node temp = head;             // temp points to box1
temp.data = 20;               // update box1's data
temp.next = new Node(30);     // create new box2 and link it
```

**Memory:**

```plaintext
[ data=20 | next--> ]  <-- box1
       ^       ^
       |       |
      head    temp
                 \
                  [ data=30 | next=null ]  <-- box2
```

**Linked List:**

```plaintext
head → 20 → 30 → null
```

---

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1759141662339/3037b0db-090f-4310-80cf-638e43b9ce4e.png align="center")

## Rule of Thumb

* **Use** `new` → When you want a **new object/box**
    
* **Do not use** `new` → When you want to **point to an existing object/box**
    

---

## Tricky Example

```java
Node temp = head;
temp = new Node(50);
```

* `temp` now points to a **new box**
    
* `head` still points to the **old box**
    

---

## Conclusion

1. `new` = create new box
    
2. **No** `new` = copy existing box’s address
    
3. Linked list traversal uses **references**, insertion uses **new**
    

### TL;DR

Understanding Java **references and** `new` keyword is just about **knowing the difference between a box’s content and its address**.

* `new` = create fresh box
    
* Reference = point to existing box
    

---
