skip to content

Object Instantiation & References

What new actually does and why a reference variable is not the object it points to. Interviewers ask this to see whether you can reason about aliasing, mutation through a shared reference, and when an object becomes garbage.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

6

What does the `new` keyword do in Java when you create an object?

level: juniorimportance: must knowfreq 78%

answer

  1. allocate heap memory
  2. default-init fields then run constructor
  3. returns a reference
  4. each new = distinct identity
  5. no new = null reference

basics

~20 s

new makes a fresh object. It sets aside memory on the heap for the object, runs its constructor to set it up, and gives you back a reference (an address) you store in a variable.

solid answer

~40 s

The `new` keyword instantiates a class: it allocates memory on the heap for a new object of that type, initializes the object's fields (defaults first, then constructor logic), runs the matching constructor, and returns a reference to the freshly created object. That reference is what you assign to a variable. So `Dog d = new Dog();` creates a Dog on the heap and stores its reference in `d`. Without `new` you only have a reference variable that points to nothing (`null`); `new` is what actually produces the object the reference points at. Each `new` call yields a distinct object with its own identity and its own copy of instance fields.

code

java · 5 lines
java
Dog d;                 // reference variable, points to nothing yet (default null for a field)
d = new Dog();         // new: allocate on heap, run constructor, assign the reference to d
Dog a = new Dog();
Dog b = new Dog();
System.out.println(a == b); // false: two distinct objects, two distinct references

go deeper

for a junior

Knows new creates an object, calls a constructor, and you store the result in a variable.

for a middle

Explains the full sequence: heap allocation, default field init, constructor (with super and initializers), then a reference is returned and assigned.

for a senior

Distinguishes reference from object, notes identity per new, and mentions objects can be created without a literal new (literals, boxing, factories).

for a principal

Discusses allocation cost, escape analysis / scalar replacement letting the JIT avoid heap allocation, and TLAB (thread-local allocation buffers) for fast bump-pointer allocation.

## What "creating an object" means In Java, a **class** is a blueprint (a template describing what fields and methods an object has). An **object** (also called an *instance*) is a concrete thing built from that blueprint, living in memory at runtime. The act of building one object from a class is called **instantiation**. The **`new` keyword** is the operator that performs instantiation. When you write: ```java Dog d = new Dog(); ``` several distinct things happen, in order: 1. **Memory allocation.** The JVM (Java Virtual Machine — the program that runs your compiled Java) reserves a chunk of memory big enough to hold one `Dog` object. This memory comes from the **heap**, a large region of RAM the JVM manages for all objects. Allocation produces the raw object. 2. **Default field initialization.** Every instance field is first set to its *default* zero-value: numeric fields become `0`, `boolean` becomes `false`, and any reference field becomes `null`. This happens before any of your code runs, so a field is never "garbage". 3. **Constructor execution.** A **constructor** is a special method with the same name as the class and no return type; its job is to set up the new object. `new Dog()` calls the no-argument `Dog` constructor. Field initializers and instance-initializer blocks also run here, after the implicit `super(...)` call to the parent class constructor. 4. **Returning a reference.** Once the object is built, `new` evaluates to a **reference** — essentially the heap address (handle) of that object. That reference is the *value* of the `new` expression. The assignment `Dog d = ...` then stores that reference into the **reference variable** `d`. `d` is not the object; `d` holds a pointer-like handle to the object on the heap. ## Why `new` matters Without `new`, you can declare a reference variable (`Dog d;`) but it points to nothing — it is `null`, or unassigned. Trying to call a method through a `null` reference throws `NullPointerException`. `new` is the bridge from "a variable that could point to a Dog" to "an actual Dog exists". Every `new` call creates a **brand-new, distinct object** with its own **identity** and its own copy of all instance fields. So `new Dog()` and `new Dog()` are two different dogs even if their fields are equal. ## Things that are *not* `new` Not every object is born from a visible `new`: - String literals (`"hi"`) create String objects via the constant pool. - Autoboxing (`Integer x = 5;`) and array literals create objects. - Factory methods (`List.of(...)`, `Integer.valueOf(...)`) create or reuse objects internally. But conceptually they all do the same heap allocation + initialization that `new` does.

  • Where does the object created by `new` live, and where does the reference variable live?
    The object lives on the heap (shared, GC-managed). A local reference variable lives on the thread's stack and holds the handle to the heap object; an instance reference field lives inside its owning heap object.
  • Does `new` always return a different object than a previous `new` of the same type?
    Yes. Each `new` expression allocates a fresh object with its own identity, so `new T() == new T()` is always false (they are different references).

new is like ordering a custom-built house from a blueprint: the builder clears a plot (allocates memory), erects the standard frame (default fields), follows your spec sheet (constructor), and hands you the address (reference). The address slip is not the house.

saying these in an interview costs you the question

  • Saying `new` returns the object itself rather than a reference to it
  • Claiming the variable IS the object
  • Thinking `new` puts the object on the stack
  • Believing two `new` calls can return the same object

context

open as a page

Explain the difference between a reference variable and the object it points to in Java.

level: juniorimportance: must knowfreq 80%

basics

~20 s

A reference variable is like a label that holds the address of an object; the object is the actual data living on the heap. Many labels can point to the same object, and a label can point to nothing (null).

open as a page

When does an object become eligible for garbage collection in Java, and what makes it eligible?

level: middleimportance: must knowfreq 72%

basics

~20 s

An object becomes eligible for garbage collection when it is no longer reachable from any live part of the running program — nothing holds a reference to it anymore. Setting all references to it to null or letting them go out of scope makes it unreachable.

open as a page

Is Java pass-by-value or pass-by-reference? Explain what happens when you pass an object to a method.

level: middleimportance: must knowfreq 85%

basics

~20 s

Java is always pass-by-value. For objects, the value copied is the reference, so the method can change the object's fields (the caller sees it), but reassigning the parameter to a new object does not affect the caller's variable.

open as a page

How does object identity differ from object equality, and how do `==`, `equals()`, and `hashCode()` relate for instantiated objects?

level: seniorimportance: must knowfreq 75%

basics

~20 s

Identity asks 'is this the exact same object?' and is tested with == on references. Equality asks 'do these represent the same value?' and is tested with equals(). Two different objects can be equal by value but never identical.

open as a page

Walk through the full lifecycle of a Java object from creation to memory reclamation.

level: seniorimportance: should knowfreq 58%

basics

~20 s

An object is created with new (memory allocated, fields defaulted, constructor run), used while reachable through references, becomes eligible for GC when no longer reachable, then is collected and its memory reclaimed at some later time.

open as a page