← Back to Home

Class & Object

Class and Object are the foundation of Object-Oriented Programming (OOP) in Java. Every Java program is built using classes and objects, making this a must-master topic for interviews and real-world development.

What Is a Class?

A class is a blueprint or template used to create objects. It defines:

  • Properties (variables / fields)
  • Behaviors (methods)
class Car {
    String color;
    int speed;

    void drive() {
        System.out.println("Car is driving");
    }
}
          

๐Ÿ‘‰ A class does not occupy memory until an object is created.

What Is an Object?

An object is a real instance of a class.

  • Represents real-world entities
  • Occupies memory
  • Can access class members
Car c = new Car();
          

Here:

  • Car โ†’ class
  • c โ†’ reference variable
  • new Car() โ†’ object creation

Real-World Analogy

Concept Real World Example
Class Blueprint of a house
Object Actual house
Variables Rooms, color
Methods Open door, lock door

Structure of a Class in Java

class ClassName {
    // variables
    dataType variableName;

    // methods
    returnType methodName() {
        // logic
    }
}
          

Example: Class & Object Program

class Student {
    String name;
    int age;

    void display() {
        System.out.println(name + " " + age);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "John";
        s1.age = 20;
        s1.display();
    }
}
          

Object Creation Process (Behind the Scenes)

Student s1 = new Student();
          

Steps:

  1. JVM loads Student class
  2. Memory allocated in heap
  3. Instance variables initialized with default values
  4. Constructor is called
  5. Reference s1 points to the object

Class Variables vs Instance Variables

class Test {
    static int x = 10;  // class variable
    int y = 20;         // instance variable
}
          
Type Memory Shared
Class variable Method area Yes
Instance variable Heap No

Multiple Objects of a Class

Student s1 = new Student();
Student s2 = new Student();
          
  • Each object has its own copy of instance variables
  • Methods are shared

Accessing Class Members

Using Object

s1.display();
          

Using Class Name (static members)

Test.x;
          

Object Reference Behavior

Student s1 = new Student();
Student s2 = s1;
          
  • Both references point to the same object
  • Changes via one reference affect the other

new Keyword Explained

  • Allocates memory
  • Creates object
  • Calls constructor
  • Returns object reference

Anonymous Object

Object without reference variable.

new Student().display();
          

โœ” Used for one-time use

Why Java Is Object-Oriented (Key Reason)

  • Code reusability
  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Common Beginner Mistakes

  • Confusing class with object
  • Accessing non-static members using class name
  • Forgetting new keyword
  • Assuming class occupies memory
  • Creating unnecessary objects

Interview-Ready Answers

Short Answer

A class is a blueprint that defines properties and behaviors, while an object is an instance of a class.

Detailed Answer

In Java, a class defines the structure and behavior of objects using variables and methods. An object is a runtime instance of a class that occupies memory and can access class members. Multiple objects can be created from a single class.

Key Takeaway

Class defines โ€œwhatโ€ an object is.

Object represents โ€œhowโ€ it exists in memory.

Mastering class and object concepts is mandatory before learning constructors, inheritance, and polymorphism.

More Class & Object Examples

1. Basic Class and Object Creation

class Car {
    String model;
}

class Demo {
    public static void main(String[] args) {
        Car c = new Car();
        c.model = "BMW";
        System.out.println(c.model);
    }
}
          

Explanation

  • Car is a class
  • c is an object

Output: BMW

2. Multiple Objects of Same Class

class Car {
    String model;
}

class Demo {
    public static void main(String[] args) {
        Car c1 = new Car();
        Car c2 = new Car();

        c1.model = "Audi";
        c2.model = "Tesla";

        System.out.println(c1.model);
        System.out.println(c2.model);
    }
}
          

Explanation

  • Each object has separate memory

Output:

Audi
Tesla

3. Object Reference Assignment

class Car {
    String model;
}

class Demo {
    public static void main(String[] args) {
        Car c1 = new Car();
        c1.model = "BMW";

        Car c2 = c1;
        c2.model = "Audi";

        System.out.println(c1.model);
    }
}
          

Explanation

  • Both references point to same object

Output: Audi

4. Object Creation Using Constructor

class Car {
    String model;

    Car(String m) {
        model = m;
    }
}

class Demo {
    public static void main(String[] args) {
        Car c = new Car("Honda");
        System.out.println(c.model);
    }
}
          

Explanation

  • Constructor initializes object

Output: Honda

5. Default Constructor Behavior

class Car {
    int price;
}

class Demo {
    public static void main(String[] args) {
        Car c = new Car();
        System.out.println(c.price);
    }
}
          

Explanation

  • Default value for int is 0

Output: 0

6. Object as Method Parameter

class User {
    String name;
}

class Demo {
    static void update(User u) {
        u.name = "Admin";
    }

    public static void main(String[] args) {
        User u = new User();
        u.name = "Guest";
        update(u);
        System.out.println(u.name);
    }
}
          

Explanation

  • Object reference passed (call by value)

Output: Admin

7. Reassigning Object Reference Inside Method

class User {
    String name;
}

class Demo {
    static void update(User u) {
        u = new User();
        u.name = "NewUser";
    }

    public static void main(String[] args) {
        User u = new User();
        u.name = "OldUser";
        update(u);
        System.out.println(u.name);
    }
}
          

Explanation

  • Reassignment is local

Output: OldUser

8. Object Array Creation

class Student {
    String name;
}

class Demo {
    public static void main(String[] args) {
        Student[] arr = new Student[2];
        arr[0] = new Student();
        arr[1] = new Student();

        arr[0].name = "A";
        arr[1].name = "B";

        System.out.println(arr[0].name + " " + arr[1].name);
    }
}
          

Explanation

  • Array holds object references

Output: A B

9. this Keyword with Class & Object

class Car {
    String model;

    Car(String model) {
        this.model = model;
    }
}

class Demo {
    public static void main(String[] args) {
        Car c = new Car("Toyota");
        System.out.println(c.model);
    }
}
          

Explanation

  • this refers to current object

Output: Toyota

10. Object Comparison Using ==

class Car {
    String model;
}

class Demo {
    public static void main(String[] args) {
        Car c1 = new Car();
        Car c2 = new Car();

        System.out.println(c1 == c2);
    }
}
          

Explanation

  • == compares references

Output: false

11. Object Comparison Using equals()

class Car {
    String model;
}

class Demo {
    public static void main(String[] args) {
        Car c1 = new Car();
        Car c2 = new Car();

        System.out.println(c1.equals(c2));
    }
}
          

Explanation

  • Default equals() behaves like ==

Output: false

12. Overriding equals() for Object Comparison

class Car {
    String model;

    public boolean equals(Object o) {
        Car c = (Car) o;
        return this.model.equals(c.model);
    }
}

class Demo {
    public static void main(String[] args) {
        Car c1 = new Car();
        Car c2 = new Car();

        c1.model = "BMW";
        c2.model = "BMW";

        System.out.println(c1.equals(c2));
    }
}
          

Explanation

  • Logical comparison

Output: true

13. Object as Return Type

class User {
    String role;
}

class Demo {
    static User createUser() {
        User u = new User();
        u.role = "Admin";
        return u;
    }

    public static void main(String[] args) {
        User u = createUser();
        System.out.println(u.role);
    }
}
          

Explanation

  • Methods can return objects

Output: Admin

14. Anonymous Object

class Demo {
    Demo() {
        System.out.println("Object created");
    }

    public static void main(String[] args) {
        new Demo();
    }
}
          

Explanation

  • No reference stored

Output: Object created

15. Multiple References to Same Object

class Demo {
    int x;
}

class Test {
    public static void main(String[] args) {
        Demo d1 = new Demo();
        Demo d2 = d1;
        Demo d3 = d2;

        d3.x = 50;
        System.out.println(d1.x);
    }
}
          

Explanation

  • All references point to same object

Output: 50

16. Object Eligible for Garbage Collection

class Demo {
    int x;
}

class Test {
    public static void main(String[] args) {
        Demo d = new Demo();
        d = null;
    }
}
          

Explanation

  • Object becomes eligible for GC

17. Object Creation Inside Loop

class Demo {
    int x;

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            Demo d = new Demo();
            d.x = i;
            System.out.println(d.x);
        }
    }
}
          

Explanation

  • New object each iteration

Output:

0
1
2

18. Class with Static and Non-Static Members

class Demo {
    static int a = 10;
    int b = 20;

    public static void main(String[] args) {
        Demo d = new Demo();
        System.out.println(a);
        System.out.println(d.b);
    }
}
          

Explanation

  • Static โ†’ class level
  • Non-static โ†’ object level

Output:

10
20

19. Object Passing Between Methods

class Box {
    int size;
}

class Demo {
    static void increase(Box b) {
        b.size += 10;
    }

    public static void main(String[] args) {
        Box b = new Box();
        b.size = 5;
        increase(b);
        System.out.println(b.size);
    }
}
          

Explanation

  • Object state modified

Output: 15

20. Interview Summary โ€“ Class & Object

class Demo {
    int x;

    static void change(Demo d) {
        d.x = 100;
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        d.x = 10;
        change(d);
        System.out.println(d.x);
    }
}
          

Explanation

  • Objects passed by value (reference copy)
  • Object state can change

Output: 100