What is the difference between a class and an object?
A class is a blueprint that describes the fields and methods something will have. An object is one real instance built from that blueprint, with its own values stored in memory.
Explain it simply
A class is the printed form for a college fest registration: it has blank fields for name, branch and phone. Each filled-in form is an object. The form design exists once; the filled forms can be thousands, each holding different data.
The class decides what data and behaviour exist. The object holds the actual values and runs the behaviour on them.
Example
class Student {
String name;
int rollNo;
Student(String name, int rollNo) { this.name = name; this.rollNo = rollNo; }
void introduce() { System.out.println(name + " (" + rollNo + ")"); }
}
Student a = new Student("Asha", 101);
Student b = new Student("Rahul", 102);
a.introduce(); // prints: Asha (101)
b.introduce(); // prints: Rahul (102)Student is written once. a and b are two separate objects, and changing a.name does not touch b.
What to say in the interview
"A class is a blueprint that defines the state and behaviour; an object is a runtime instance of it with its own copy of the state. One class can create many objects. In Java the class is loaded once, while each new allocates a fresh object on the heap."
Common mistakes
- Saying a class takes memory for each field. Only objects do; the class itself is just a definition (plus static data).
- Confusing the reference variable with the object. a is a reference that points to the object.