← Back to Home

Object Creation & Memory

Object creation and memory management explain how Java creates objects, where they are stored, and how references work at runtime. This topic is crucial for debugging, performance, interviews, and understanding JVM internals.

What Happens When an Object Is Created?

Student s = new Student();
          

Step-by-Step Execution Flow

  1. Class Loading
    • JVM loads Student.class into memory (if not already loaded)
    • Class metadata stored in Method Area / Metaspace
  2. Memory Allocation
    • Memory allocated in the Heap for the object
    • Space created for all instance variables
  3. Default Initialization
    • Instance variables get default values
    • int → 0
    • boolean → false
    • Object → null
  4. Constructor Execution
    • Constructor initializes instance variables with actual values
  5. Reference Assignment
    • Reference variable s (stored in stack) points to heap object

Java Memory Areas (High-Value Interview Topic)

1. Stack Memory

  • Stores local variables, method calls, and references
  • Memory is method-scoped
  • Automatically freed after method execution
Student s; // reference stored in stack
          

2. Heap Memory

  • Stores objects and instance variables
  • Shared across threads
  • Managed by Garbage Collector
new Student(); // object stored in heap
          

3. Method Area / Metaspace

  • Stores:
  • Class structure
  • Method bytecode
  • Static variables
  • Runtime constant pool

Visual Representation (Conceptual)

Stack
-----------------
s  ----->  Heap
           -----------------
           Student Object
           name = "John"
           age  = 20
          

Object vs Reference (Very Important)

Student s1 = new Student();
Student s2 = s1;
          
  • Only one object created in heap
  • s1 and s2 both point to same object
  • Changes via one reference affect the other

Multiple Objects Creation

Student s1 = new Student();
Student s2 = new Student();
          
  • Two separate objects in heap
  • Each has its own instance variables

Anonymous Objects

new Student().display();
          
  • No reference variable
  • Used for one-time use
  • Eligible for garbage collection immediately after use

Static vs Instance Memory Allocation

class Test {
    static int x = 10;
    int y = 20;
}
          
Member Type Memory Location Shared
Static variable Method Area Yes
Instance variable Heap No
Local variable Stack No

Garbage Collection (Brief Intro)

  • Objects with no references become eligible for GC
  • GC automatically frees heap memory
  • Programmer cannot explicitly delete objects
s = null; // object eligible for GC
          

Common Memory Scenarios (Interview-Oriented)

Case 1: Reference Reassignment

Student s = new Student();
s = new Student();
          
  • First object becomes eligible for GC
  • Second object is now referenced

Case 2: Reference as Method Parameter

void change(Student s) {
    s.name = "Alex";
}
          
  • Copy of reference passed
  • Object content changes
  • Still call by value

Common Beginner Mistakes

  • Confusing reference with object
  • Thinking reference stores object data
  • Forgetting heap vs stack separation
  • Assuming GC runs immediately
  • Creating unnecessary objects

Interview-Ready Answers

Short Answer

Object creation in Java involves allocating memory in the heap and storing the reference in the stack.

Detailed Answer

When an object is created in Java, the class is loaded, memory is allocated in the heap, instance variables are initialized, and the constructor is executed. A reference to the object is stored in stack memory. Java manages memory using stack, heap, and method area, with garbage collection handling unused objects.

Key Takeaway

Objects live in the heap. References live in the stack. Classes live in the method area. Understanding object creation and memory flow is essential for efficient coding, debugging, and JVM-level clarity.