Classes and Objects in Java
Classes and objects in Java are the foundation of object-oriented programming (OOP). Every real-world Java application is built using classes and objects, making this one of the most important topics for Java beginners.
In this beginner-friendly tutorial, you will learn what a Java class is, what a Java object is, and how to create objects in multiple ways with simple real-world examples.
- Class: A blueprint used to create objects.
- Object: A real instance 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:
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.
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
Class<?> clazz = Class.forName("Building");
Building building = (Building) clazz.getDeclaredConstructor().newInstance();Explanation:
Class.forName("Building")→ loads the class using its namegetDeclaredConstructor().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
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, Blue4. 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.
ObjectInputStream in = new ObjectInputStream(new FileInputStream("building.ser"));
Building building = (Building) in.readObject();
in.close();Output:
Building with 5 floorsSummary
| Method | When to Use | Key Feature |
|---|---|---|
| new keyword | Standard object creation | Most common way |
| Reflection | Frameworks, dynamic loading | Runtime class loading |
| clone() | Copying existing objects | Creates duplicate |
| Deserialization | Loading saved objects | From byte stream |
Standard object creation
Most common way
Frameworks, dynamic loading
Runtime class loading
Copying existing objects
Creates duplicate
Loading saved objects
From byte stream
Classes and Objects in Java – FAQ for Beginners
What is a class in Java?
A class in Java is a blueprint or template used to create objects. It defines properties (variables) and behaviors (methods).
What is an object in Java?
An object is a real instance of a class. It represents a real-world entity created from the class blueprint.
What is the difference between class and object in Java?
A class is a blueprint, while an object is a real instance created from that blueprint.
How many ways can we create objects in Java?
Objects in Java can be created using the new keyword, reflection, clone() method, and deserialization.
