🏠
OOP

Class & Object

Blueprint vs Real Thing
💡 A class is a blueprint for a house. An object is a real house built from that blueprint — and you can build many houses from one blueprint!

A class defines what properties (fields) and what behavior (methods) an object will have.

An object is an "instance" of a class — a real thing you can use, with its own values.

class Car {
  String color;
  void drive() { System.out.println(color + " car driving!"); }
}
Car myCar = new Car();
myCar.color = "Red";
myCar.drive();
🏠
A class is a blueprint for a house. An object is a real house built from that blueprint — and you can build many houses from one blueprint!
1 / 6
⚡ Quick Recap
  • Class = blueprint
  • Object = a real instance
  • The new keyword creates an object
On this page (3 subtopics)

Fields (variables) are a class's "state" — data the object holds onto (like a Car's color, speed). Methods are "behavior" — things the object can do (like a Car's drive(), brake()). A class is a combination of both: "what this thing is" (fields) and "what it can do" (methods).

Fields are also called "attributes" or "properties", and methods "behaviors" or "functions". In real-world modeling, whenever you design a class, ask yourself two questions: "what information will this thing have?" (fields) and "what all can this thing do?" (methods).

💡Tip: When designing a class, first think of a real-world noun (Car, Student, Order), then list its adjectives (fields — color, name, total) and verbs (methods — drive, study, checkout).

Different objects built from the same class hold their own independent field values — changing one object's value has no effect on another. This is because every "new" call allocates a new, separate memory block (in the Heap).

Static fields are an exception to this rule (as you'll see in detail in the "this-static" topic) — they're class-level, so they're shared across all objects, and a change is visible in all of them.

Car car1 = new Car();
Car car2 = new Car();
car1.color = "Red";
car2.color = "Blue";  // car1.color abhi bhi "Red" hi hai
System.out.println(car1 == car2); // false — do alag objects hain

When you write "new Car()", this exactly happens, step-by-step: (1) space is allocated in Heap memory for the object (all its instance fields), (2) fields get default values (0, null, false), (3) the constructor runs, giving the fields meaningful starting values, (4) a "reference" (memory address) is returned, which gets stored in the variable (which itself lives on the Stack, if it's a local variable).

So the variable itself is NOT the object — it's just a reference/address pointing toward the real thing sitting in the Heap. This is why, when you assign an object (car2 = car1), both variables start pointing to the same object (no copy is made, only the address is copied) — this is called "aliasing".

StackHeap
What's storedLocal variables, referencesActual objects, instance fields
LifetimeGone when the method endsUntil no reference remains, then GC
SpeedFastA bit slower (GC overhead)
⚠️Common Mistake: car2 = car1; does NOT make a copy — both now point to the same object. If you change car2.color, car1.color changes too! For a real copy, you have to build a new object yourself.