OOP.In which parts of JVM Memory class, object, interface stored?

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 AreaPurpose
Method Area (part of Metaspace in modern JVMs)Stores class metadata, including class structures, method signatures, static fields, and interfaces.
HeapStores objects (instances) and their instance fields. This is the biggest part of memory.
StackStores local variables (primitive values + references to objects) for each method call. Each thread gets its own stack.

🔗 Where Do Class, Object, and Interface Fit?

ConceptStored InExplanation
ClassMethod 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.
ObjectHeapWhen 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.
InterfaceMethod 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

StepWhat 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.

🔥 What About Interfaces?

interface Payment {
    void processPayment(double amount);
}
  • 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)

🔗 Quick Recap

ConceptWhere?Example
Class MetadataMethod Areaclass name, method signatures, static fields
Interface MetadataMethod Areainterface name, method signatures, constants
Objects (Instances)Heapactual data: fields like brand
Local References & VariablesStackCar car = new Car();car is in Stack
This entry was posted in Без рубрики. Bookmark the permalink.