Java Academy Logo

Classes and Objects in Java

Classes and objects are the foundation of object-oriented programming. They help to structure real-world entities in our code base.

  • Classes: Blueprint that creates objects. Shares common properties of the object.
  • Objects: Specific entity that is created from a class.

Real Life Analogy: Building Blueprint

Class = Blueprint

A building blueprint defines:

  • How many windows
  • How many floors
  • Type of materials
  • Layout

But a blueprint is not a building — it's only the plan.

Object = Real Building

Objects are real buildings that are built using the blueprint.

✔ Class = Blueprint, Object = Real Building

Java Class (Blueprint)

The Building class is like a blueprint:

Java Class (Blueprint)
class Building {
    int floors;
    String color;
    
    Building(int floors, String color) {
        this.floors = floors;
        this.color = color;
    }
}

Object = Real Building

Objects are created from the class using the new keyword.

objectExample.java
Building building1 = new Building(5, "Blue");
Building building2 = new Building(10, "White");

System.out.println("This building has " + building1.floors + " floors and is " + building1.color + ".");
System.out.println("This building has " + building2.floors + " floors and is " + building2.color + ".");

Output:

This building has 5 floors and is Blue.This building has 10 floors and is White.

Ways to Create Objects

There are four main ways to create objects in Java:

1. Using new keyword

Most common way

2. Using Reflection

Create objects dynamically

3. Using clone() method

Copy existing objects

4. Using Deserialization

Recreate from byte stream

1. Using new Keyword

Most common way to create an object.

Building building1 = new Building(5, "Blue");

2. Using Reflection

Create an object without using the new keyword, by asking Java to create it at runtime using the class name.

Useful for:

  • Frameworks (Spring, Hibernate)
  • Loading classes dynamically
  • Working with unknown classes at runtime
reflectionExample.java
Class<?> clazz = Class.forName("Building");
Building building = (Building) clazz.getDeclaredConstructor().newInstance();

Explanation:

  • Class.forName("Building") → loads the class using its name
  • getDeclaredConstructor().newInstance() → creates object
  • No new Building() used!

3. Using clone() Method

The clone() method creates a copy of an existing object instead of creating a new one manually.

Steps to Use clone():

  • Your class must implement Cloneable
  • You must override the clone() method
  • Then you can copy the object
cloneObjectExample.java
class Building implements Cloneable {
    int floors;
    String color;
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

Building original = new Building(5, "Blue");
Building cloned = (Building) original.clone();

Output:

Original: 5, BlueClone: 5, Blue

4. Using Deserialization

Deserialization is a technique of reading an object from the saved state in a file. The object is recreated from a stored byte stream.

desenterlizationExample.java
ObjectInputStream in = new ObjectInputStream(new FileInputStream("building.ser"));
Building building = (Building) in.readObject();
in.close();

Output:

Building with 5 floors

Summary

new keyword
When to Use:

Standard object creation

Key Feature:

Most common way

Reflection
When to Use:

Frameworks, dynamic loading

Key Feature:

Runtime class loading

clone()
When to Use:

Copying existing objects

Key Feature:

Creates duplicate

Deserialization
When to Use:

Loading saved objects

Key Feature:

From byte stream