Java Classes & Objects: Blueprint Definition & Heap Memory Allocation

In Java, classes and objects form the structural foundation of all application logic. A class acts as the blueprint, and an object is the physical living instance produced from that blueprint.

Class Blueprint vs Heap Object Instantiation

Figure 6: Memory Trace: Class Template in Metaspace, Reference Variable on Call Stack, and Object Instances in Heap.

1. Defining a Class (The Blueprint)

A class definition groups fields (state) and methods (behavior):

public class Car {
    // 1. State / Attributes (Instance Variables)
    String brand;
    String color;
    int currentSpeed;

    // 2. Behavior / Actions (Methods)
    public void accelerate(int speedIncrease) {
        currentSpeed += speedIncrease;
        System.out.println(brand + " accelerated to: " + currentSpeed + " km/h");
    }

    public void applyBrakes() {
        currentSpeed = 0;
        System.out.println(brand + " has come to a complete halt.");
    }
}

2. Instantiating Objects using the new Keyword

To bring a class to life, you instantiate an object using the new operator. Each object possesses its own independent copy of instance variables:

public class CarShowroom {
    public static void main(String[] args) {
        // Step 1: Create first Car object
        Car car1 = new Car();
        car1.brand = "Toyota Fortuner";
        car1.color = "Pearl White";
        car1.currentSpeed = 60;

        // Step 2: Create second Car object
        Car car2 = new Car();
        car2.brand = "Tesla Model 3";
        car2.color = "Midnight Silver";
        car2.currentSpeed = 85;

        // Step 3: Invoke methods via dot operator (.)
        car1.accelerate(20); // Toyota accelerated to 80 km/h
        car2.applyBrakes();   // Tesla has come to a complete halt
    }
}

Memory Trace: Stack vs. Heap Allocation

  • Call Stack: When Car car1 is declared inside main(), the JVM allocates 4 or 8 bytes on the Call Stack for the local variable car1. Initially, it holds a memory address pointer (e.g. 0x4A12).
  • Heap Memory: The expression new Car() allocates contiguous memory on the Heap for the Car instance. The fields (brand, color, currentSpeed) reside inside this Heap block.

University Exam Corner (BCA / B.Sc CS)

Exam Question: "What is the output of the following code?"

Car c1 = new Car();
c1.brand = "Ford";
Car c2 = c1;
c2.brand = "BMW";
System.out.println(c1.brand);

Output: BMW
Explanation: In Java, assigning an object variable (Car c2 = c1;) does NOT copy the object! It copies the reference pointer. Both c1 and c2 point to the exact same memory address on the Heap. Modifying c2.brand directly mutates the shared object.

Next Step

Now let us explore constructors, default constructors, and constructor chaining. Proceed to: Java Constructors & Chaining →

Frequently Asked Questions (FAQ)

Q1. What actually happens in memory when 'new' is executed in Java?
When 'new' executes: 1. The JVM calculates the memory size required for all instance variables, 2. Allocates contiguous memory space on the Heap, 3. Zeroes out fields to default values, 4. Invokes the constructor to initialize state, and 5. Returns the 64-bit Heap address pointer back to the Stack reference variable.
Q2. Can a class exist without an object? Can an object exist without a class?
A class exists in Metaspace as a bytecode template independently of whether any objects are instantiated. An object CANNOT exist without a class because it is instantiated strictly from the class blueprint.
Q3. What is an Anonymous Object in Java?
An anonymous object is an object instantiated without storing its reference address into a named variable (e.g. 'new Calculation().fact(5);'). It is used for one-off method executions and is immediately eligible for garbage collection.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java Constructors →

Initializing object state.

The 'this' Keyword →

Referencing the current object.

JVM Heap & Stack →

Object lifecycle and GC.

Encapsulation & Getters →

Protecting object fields.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!