Let’s walk through how classes, objects, and interfaces are stored in the JVM memory model — this is important not just for OOP understanding, but also for performance tuning and debugging.
📦 JVM Memory Structure (High Level)
The JVM divides memory into several regions, but the most important ones for our question are:
Memory Area
Purpose
Method Area (part of Metaspace in modern JVMs)
Stores class metadata, including class structures, method signatures, static fields, and interfaces.
Heap
Stores objects (instances) and their instance fields. This is the biggest part of memory.
Stack
Stores local variables (primitive values + references to objects) for each method call. Each thread gets its own stack.
🔗 Where Do Class, Object, and Interface Fit?
Concept
Stored In
Explanation
Class
Method Area (Metaspace)
When a class is loaded by the ClassLoader, its metadata (class name, method signatures, static variables, parent class, etc.) is stored in the Method Area.
Object
Heap
When you create an object (using new), the actual object data (its fields/attributes) is stored in the Heap. A reference to this object might be stored in the Stack if it’s a local variable.
Interface
Method Area (Metaspace)
Just like classes, interface metadata (its name, method signatures, constants) is stored in the Method Area when the interface is loaded by the ClassLoader.
💡 Example to Bring It All Together
public class Car { // Class loaded into Method Area (Metaspace)
String brand; // Instance variable (in the Heap when object is created)
static int totalCars; // Static variable (stored in Method Area)
public void drive() { // Method info (signature, modifiers) - stored in Method Area
int speed = 60; // Local variable - stored in Stack
}
}
Car car = new Car(); // 'car' (reference) stored in Stack
// Actual Car object (fields like 'brand') stored in Heap
🚀 Summary Flow
Step
What Happens?
1️⃣
Car class gets loaded into the Method Area (including its method signatures and static variables).
2️⃣
car = new Car() creates an object in the Heap.
3️⃣
car is a reference stored in the Stack (local variable).
4️⃣
brand (non-static field) exists inside the object in the Heap.
5️⃣
totalCars (static field) stays in the Method Area.
When Payment is loaded, its metadata (method signatures, constants) is stored in Method Area.
When a class like CreditCardPayment implements this interface, that class’s metadata (along with its own methods) also goes to Method Area.
Actual objects implementing the interface (like CreditCardPayment objects) go into the Heap when instantiated.
📊 Visualization
Method Area (Metaspace)
|-> Car class metadata
|-> Payment interface metadata
|-> Static fields
Heap
|-> car object (brand field inside it)
|-> Other objects
Stack (each thread gets one)
|-> car reference (local variable in main method)