Skip to main content

Command Palette

Search for a command to run...

02.Types of Objects in Java

Published
•3 min read•View as Markdown

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 new keyword.

  • 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:

    1. Declare class final

    2. Make fields private and final

    3. No setters, only getters

    4. 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

TypeDescriptionKey Feature
Regular ObjectNormal object using newMultiple instances possible
Anonymous ObjectNo referenceUsed once, immediately
Singleton ObjectOnly one instanceGlobal access point
Immutable ObjectState cannot changeThread-safe
Cloned ObjectCopy of existing objectShallow/deep copy
Deserialized ObjectRecreated from byte streamPersistence/network

More from this blog

SS-JAVA

23 posts