A class is a blueprint. An object is a real instance created from that blueprint.

Class

A class defines:

The class itself describes structure. Object data exists when an object is created.

Object

An object is an instance of a class. When an object is created:

Example

class FeatureFlag {
    String key;
    boolean enabled;

    void disable() { enabled = false; }
}

FeatureFlag darkMode = new FeatureFlag();

Here:

Multiple Objects

One class can create many objects.

Object Key Enabled
darkMode dark-mode true
betaSearch beta-search false
newEditor new-editor true

The structure is the same, but each object has its own state.

Stack vs Heap

Stack                     Heap
darkMode reference  --->   FeatureFlag object

Local references often live on the stack. Actual objects usually live on the heap.

Object Lifecycle

Class loaded
Object created
Constructor initializes object
Object is used
Object becomes unreachable
Garbage collector can reclaim memory

Interview Notes