Class vs Object in Java
Fast recall
Quick revision
Core answer
Whether you understand that a class is a template and an object is a live instance — and that each object has its own independent data.
Using 'class' and 'object' interchangeably, or thinking all objects share the same field values.
Mention that the class declares the instance fields and behavior, but each object allocates its own heap memory at runtime — and only static fields are shared.
Interview-ready guide
Interview answer
A class is the declaration of a type—its fields, constructors, and methods. An object is one runtime instance of that class. One Student class can therefore have many Student objects, each with different name and marks values.
Declaring Student s does not create an object. After s = new Student(...), the variable refers to an instance. If I then write Student other = s, Java copies the reference, so there is still one object and changes through either reference are visible through the other.
Class vs Object in Java
Direct Answer
A class declares a type's fields, constructors, and methods. An object is a runtime instance of a class with its own instance-field state. One class can have many objects; static members are associated with the loaded class rather than copied into each object.
Code Example
// The class — a blueprint, no memory allocated here yet
class Student {
String name;
int marks;
void printGrade() {
System.out.println(name + ": " + (marks >= 50 ? "Pass" : "Fail"));
}
}
public class Main {
public static void main(String[] args) {
// Two objects from the same class — fully independent
Student s1 = new Student();
s1.name = "Alice";
s1.marks = 95;
Student s2 = new Student();
s2.name = "Bob";
s2.marks = 42;
s1.printGrade(); // Alice: Pass
s2.printGrade(); // Bob: Fail
// Proof of independence
s1.marks = 60;
System.out.println(s2.marks); // 42 — unaffected
}
}
Same class, two objects, completely independent data.
Common Mistakes & Pitfalls
- Using "class" and "object" as if they mean the same thing — the class is the definition you write, the object is one live instance built from it; the interviewer hears the slip the moment you make it.
- Assuming all objects of a class share their field values — each object gets its own copy of the instance fields, so changing
s1.marksnever touchess2.marksunless that field isstatic. - Believing
Student s;already creates an object — it only creates anullreference; the object exists only afternew, and touchingsbefore that throwsNullPointerException. - Thinking the class takes heap memory — only objects live on the heap; the class is loaded once as a template, so "how many objects" and "how many classes" are completely different questions.
Check your understanding
Answer these without looking back at the article.
- Where are objects stored in memory in Java?
- What is the difference between a reference variable and the object it points to?
- What is the difference between instance and static members?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.