Difference Between Object and Class

Naveen AnamaneniUpdated · 5 min read

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

ClassObject
What it isA blueprint or templateAn instance built from the class
Created byWriting code: class BankAccount { ... }new BankAccount(...) at runtime
How manyOne per classAny number per class
Loaded or createdLoaded once by the class loaderCreated each time new runs
Lives in memoryClass data in the method area (Metaspace)On the heap
HoldsThe field definitions, method code, static fieldsIts own values for the instance fields
ExampleBankAccountNavika's account with Rs. 5,000

In Memory

Class loaded once; objects created many times
Accounts.java▶ Try it
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());
    }
}
Output
Accounts: 3
Navika Srihitha Advitha
true

How it works

  1. One class, BankAccount, is loaded once. Its static field totalAccounts lives with the class, so there's only one counter.
  2. Three new calls create three objects, each with its own holder and balance.
  3. a1.getClass() == a3.getClass() is true: 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:

ExpressionResult here
a1.getClass().getSimpleName()"BankAccount"
a1 instanceof BankAccounttrue
a1 == a2false: 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

  1. Easy: For a Movie class in a ticket app, give three examples of objects and say which values differ between them.
  2. Harder: Add a static count to a class Student, create four objects, and print the count using the class name, not an object.

Quick check: Object vs class

4 questions
1How many times is a class loaded in a running program, normally?
2Where do objects live?
3What does each object hold?
4a1.getClass() == a3.getClass() for two BankAccount objects is:

Key Takeaways

Was this tutorial helpful?

Lesson 94 of 482