Difference Between Object and Class
A mould for making clay diyas isn't a diya; you can't light it. But from one mould a potter can make a thousand diyas, each a little different in color. Class and object have exactly this relationship, and "what's the difference between a class and an object?" is asked in almost every fresher interview. This lesson puts them side by side, including what happens in memory.
Side by Side
| Class | Object | |
|---|---|---|
| What it is | A blueprint or template | An instance built from the class |
| Created by | Writing code: class BankAccount { ... } | new BankAccount(...) at runtime |
| How many | One per class | Any number per class |
| Loaded or created | Loaded once by the class loader | Created each time new runs |
| Lives in memory | Class data in the method area (Metaspace) | On the heap |
| Holds | The field definitions, method code, static fields | Its own values for the instance fields |
| Example | BankAccount | Navika's account with Rs. 5,000 |
In Memory
public class Accounts {
static class BankAccount {
String holder;
long balance;
static int totalAccounts = 0;
BankAccount(String holder, long balance) {
this.holder = holder;
this.balance = balance;
totalAccounts++;
}
}
public static void main(String[] args) {
BankAccount a1 = new BankAccount("Navika", 5000);
BankAccount a2 = new BankAccount("Srihitha", 12000);
BankAccount a3 = new BankAccount("Advitha", 800);
System.out.println("Accounts: " + BankAccount.totalAccounts);
System.out.println(a1.holder + " " + a2.holder + " " + a3.holder);
System.out.println(a1.getClass() == a3.getClass());
}
}Accounts: 3
Navika Srihitha Advitha
trueHow it works
- One class,
BankAccount, is loaded once. ItsstaticfieldtotalAccountslives with the class, so there's only one counter. - Three
newcalls create three objects, each with its ownholderandbalance. a1.getClass() == a3.getClass()istrue: different objects, but the same class.
Try it yourself: print a1 == a3. Same class, but are they the same object?
Every Object Knows Its Class
Every object can tell you its class with getClass(), and instanceof checks whether an object belongs to a class:
| Expression | Result here |
|---|---|
a1.getClass().getSimpleName() | "BankAccount" |
a1 instanceof BankAccount | true |
a1 == a2 | false: two different objects |
Common Mistakes
Saying a class "has a value". A class defines which fields exist; each object has its own values. Only static fields have a value at class level.
Thinking each object gets a copy of the methods. Method code lives once, with the class; objects only hold field values.
Comparing objects of the same class with == to check their contents. == checks whether they're the same object. Content comparison uses equals (see "Object Equality and Hash Codes in Java").
Interview Corner
What is the difference between a class and an object?
A class is a blueprint that defines fields and methods; it's written once and loaded once into the method area. An object is an instance of that class created at runtime with new; it lives on the heap and holds its own values for the instance fields. One class can have many objects.
Does each object have its own copy of the methods?
No. Method code is stored once with the class. Each object holds only its own instance field values; when a method runs, it works on the fields of the object it was called on.
Practice
- Easy: For a
Movieclass in a ticket app, give three examples of objects and say which values differ between them. - Harder: Add a static
countto a classStudent, create four objects, and print the count using the class name, not an object.
Key Takeaways
Was this tutorial helpful?