What new Does in Memory
Fast recall
Quick revision
Core answer
new Student(...) creates a new object, initializes its instance fields to default values, runs the selected constructor (including the superclass constructor chain), and produces a reference to the object. The Java language does not expose the object's numeric address or require the local reference to occupy a particular stack location.Whether you understand the difference between stack (reference) and heap (object data), and the 4-step sequence that new triggers.
Thinking the object lives on the stack, or that the variable and the object are the same thing. The variable is just a reference; the object is on the heap.
Mention that every object carries a hidden object header (metadata used by the JVM for garbage collection and type identification), so the actual memory footprint is slightly larger than just the fields.
Interview-ready guide
Interview answer
new Student("Asha") creates a new Student object, gives its fields their default values, performs superclass and instance initialization in Java's defined order, and then runs the selected constructor. The result of the expression is a reference to that object, which can be stored in a variable such as Student first.
For example, after Student second = first, Java has not copied the Student; both variables refer to the same object. Changing second.name is therefore visible through first. Reassigning second to another object does not change what first refers to. Exact placement and raw addresses are JVM implementation details, so the portable explanation is about object creation, initialization, and reference values rather than a promised stack-versus-heap diagram.
What `new` Does in Memory
Direct Answer
Evaluating new Student(...) creates a new object, initializes its instance fields to default values, runs the selected constructor (including the superclass constructor chain), and produces a reference to the object. The Java language does not expose the object's numeric address or require the local reference to occupy a particular stack location.
Code Example
This short program proves the order of events. Before the constructor sets anything, the fields already hold their defaults — and two references to the same new object see each other's changes.
public class NewKeywordDemo {
public static void main(String[] args) {
// Step 1+2 already done before this line finishes:
// heap space reserved, fields default-filled (name=null, marks=0).
// Step 3: constructor sets real values. Step 4: reference assigned in s1.
Student s1 = new Student("Alice", 95);
// A second reference to the SAME heap object — no new object created
Student s2 = s1;
s2.marks = 100;
System.out.println(s1.marks); // 100 — s1 and s2 point to one object
// Setting s1 = null only drops the reference; the object still existed
s1 = null;
System.out.println(s2.marks); // 100 — object survives via s2
}
}
class Student {
String name;
int marks; // default-filled to 0 before the constructor runs
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
}
One new call, one heap object — and null-ing one reference never touches the data another reference still holds.
Common Mistakes & Pitfalls
- Thinking the object lives on the stack — only the reference (the address) sits on the stack; the actual object data always lives on the heap, and confusing the two is the single most common error here.
- Believing the constructor creates the object — heap allocation and default-filling happen first; by the time the constructor runs the object already exists, and the constructor only sets its initial values.
- Assuming
s1 = nulldeletes the object — it just removes one reference; the object survives on the heap until no reference points to it and the garbage collector reclaims it. - Reading
Student s2 = s1as a copy — it copies the address, not the object, so both variables drive the same heap object and a change through one is visible through the other.
Check your understanding
Answer these without looking back at the article.
- What is the difference between the stack and the heap in Java?
- What does the garbage collector do with orphaned objects?
- What happens if the constructor throws an exception — is the object created?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.