0
0
JavaConceptBeginner · 3 min read

What is Singleton Class in Java: Explanation and Example

A singleton class in Java is a class designed to have only one instance throughout the program. It ensures that only one object of the class is created and provides a global point of access to that object.
⚙️

How It Works

A singleton class works by controlling the creation of its instance. Instead of allowing multiple objects to be created, it keeps a single private static variable that holds the one and only instance.

Think of it like a single remote control for your TV. You don’t want many remotes causing confusion; just one remote controls everything. Similarly, the singleton class provides one object that everyone uses.

To achieve this, the class hides its constructor (makes it private) so no other class can create a new object. Instead, it offers a public method that returns the single instance, creating it only if it doesn’t exist yet.

💻

Example

This example shows a simple singleton class in Java with a method to display a message. The main method gets the instance twice and shows that both references point to the same object.

java
public class Singleton {
    private static Singleton instance; // single instance

    private Singleton() {
        // private constructor hides object creation
    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton(); // create only once
        }
        return instance;
    }

    public void showMessage() {
        System.out.println("Hello from the Singleton!");
    }

    public static void main(String[] args) {
        Singleton obj1 = Singleton.getInstance();
        Singleton obj2 = Singleton.getInstance();

        obj1.showMessage();

        if (obj1 == obj2) {
            System.out.println("Both references are the same instance.");
        } else {
            System.out.println("Different instances exist.");
        }
    }
}
Output
Hello from the Singleton! Both references are the same instance.
🎯

When to Use

Use a singleton class when you need exactly one object to coordinate actions across the system. For example:

  • Managing a connection to a database so all parts of the program share the same connection.
  • Controlling access to a shared resource like a printer or a file.
  • Implementing logging where all messages go through one logger instance.

Singletons help avoid conflicts and save resources by preventing multiple instances that do the same job.

Key Points

  • A singleton class restricts instantiation to one object.
  • It uses a private constructor and a static method to control instance creation.
  • Provides a global access point to the single instance.
  • Commonly used for shared resources or configurations.

Key Takeaways

A singleton class ensures only one instance exists in the program.
It hides its constructor and provides a static method to get the instance.
Singletons are useful for shared resources like database connections or loggers.
Use singleton carefully to avoid issues in multithreaded environments.
Singleton provides a global point of access to the single object.