OOP · Building Blocks of OOP
What is an Object?
A class only defines the structure of an entity.
Introduction
A class only defines the structure of an entity. To use that structure in a program, we need to create an object. An object is a real instance of a class that contains actual data and can perform the actions defined in the class.
Definition: An object is an instance of a class that stores actual values and uses the methods defined by the class. Why Do We Need an Object? A class acts like a template, but it cannot store individual information.
For example, a Student class defines that every student has a name, age, and roll number. To represent actual students like Rahul, Priya, or Arun, we create separate objects from the class. Without objects, a class is only a blueprint and cannot represent real data.
Real-World Example
Imagine a car showroom.
- Car Class → Defines the design of a car.
- Car Object 1 → Red Honda City
- Car Object 2 → White Hyundai i20
- Car Object 3 → Black Tata Nexon
All these cars follow the same design but contain different values.
1. Real Instance
A class is a blueprint, while an object is the real instance created from that blueprint. For example, a Student class defines that every student has a name, age, and roll number. When we create s1, it becomes an actual student object with its own data.
Example
Here, s1 and s2 are two different objects created from the same class.
2. Memory Creation
When an object is created using the new keyword, Java allocates memory for that object.
Student s1 = new Student();

During this process:
- A new object is created.
- Memory is allocated.
- The object is initialized.
- A reference to the object is returned.

3. Heap Memory
All objects in Java are stored in the Heap Memory. The heap is a large memory area used for storing objects and their data during program execution.

Example
Student s1 = new Student(); The actual Student object is created inside the Heap Memory.
4. Reference Variable
The object itself is stored in the heap, but we access it using a reference variable.
Student s1 = new Student(); Here:
- Student → Class
- new
Student()→ Creates the object - s1 → Reference variable
The reference variable stores the memory address of the object, not the object itself.
5. Stack Memory
Reference variables are stored in the Stack Memory.

The stack stores local variables and references, while the heap stores the actual objects.
6. Stack vs Heap


7. Object Lifecycle
Every object goes through four stages.
Step 1 – Declaration
Student s1; Only the reference variable is created.
Step 2 – Creation
s1 = new Student(); Memory is allocated in the heap, and the object is created.
Step 3 – Usage
s1.study(); The object performs actions and stores data.
Step 4 – Destruction
When the object is no longer referenced, Java's Garbage Collector (GC) automatically removes it from the heap.
s1 = null; Now the object becomes eligible for garbage collection.

An object is a concrete instance created from a class blueprint, with its own values.
public class Demo {
public static void main(String[] args) {
Car tesla = new Car(); // 'new' builds an object from the blueprint
tesla.model = "Model 3";
tesla.speed = 0;
tesla.accelerate(60);
System.out.println(tesla.model + " @ " + tesla.speed); // Model 3 @ 60
}
}