02.Types of Objects in Java
Types of Objects in Java. Objects are instances of classes, but they can be categorized based on how they are created, their lifecycle, and behavior.
1. Regular / Normal Object
Definition: Standard object created using the
newkeyword.Characteristics: Stored in heap memory; can have multiple instances.
Example:
class Car {
String brand;
}
public class Main {
public static void main(String[] args) {
Car c1 = new Car(); // Regular object
Car c2 = new Car();
}
}
2. Anonymous Object
Definition: Object without a reference variable; used when the object is needed only once.
Characteristics: Cannot be reused; often used in method calls.
Example:
class Car {
void drive() {
System.out.println("Driving...");
}
}
public class Main {
public static void main(String[] args) {
new Car().drive(); // Anonymous object
}
}
3. Singleton Object
Definition: Only one instance of the class exists in the application.
Characteristics: Used for resource management, logging, configuration.
Example:
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2); // true
}
}
4. Immutable Object
Definition: Object whose state cannot change after creation.
Characteristics: Thread-safe by default; often used for Strings and wrapper classes.
Rules to Create Immutable Class:
Declare class
finalMake fields
privateandfinalNo setters, only getters
Initialize fields via constructor
Example:
final class Person {
private final String name;
private final int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
5. Object via Cloning
Definition: Object created as a copy of another object using
clone()method.Characteristics: Shallow copy by default; deep copy requires custom implementation.
Example:
class Car implements Cloneable {
String brand;
Car(String brand) { this.brand = brand; }
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Main {
public static void main(String[] args) throws Exception {
Car c1 = new Car("BMW");
Car c2 = (Car)c1.clone();
System.out.println(c1.brand + " " + c2.brand);
}
}
6. Object via Deserialization
Definition: Object recreated from a byte stream (saved file or network).
Characteristics: Used to persist objects or send over network.
✅ Summary Table
| Type | Description | Key Feature |
| Regular Object | Normal object using new | Multiple instances possible |
| Anonymous Object | No reference | Used once, immediately |
| Singleton Object | Only one instance | Global access point |
| Immutable Object | State cannot change | Thread-safe |
| Cloned Object | Copy of existing object | Shallow/deep copy |
| Deserialized Object | Recreated from byte stream | Persistence/network |