How to Create Singleton Class in Java: Simple Guide
To create a
singleton class in Java, make the constructor private and provide a static method that returns the single instance. This ensures only one object of the class exists during the program runtime.Syntax
A singleton class in Java typically has a private static variable to hold the single instance, a private constructor to prevent outside instantiation, and a public static method to provide access to the instance.
- private static ClassName instance; - holds the single object.
- private ClassName() - prevents creating new objects from outside.
- public static ClassName getInstance() - returns the single instance, creating it if needed.
java
public class Singleton { private static Singleton instance; private Singleton() { // private constructor } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
Example
This example shows a singleton class with a method to display a message. It demonstrates that multiple calls to getInstance() return the same object.
java
public class Singleton { private static Singleton instance; private Singleton() { // private constructor } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } public void showMessage() { System.out.println("Hello from 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 point to the same instance."); } else { System.out.println("Different instances exist."); } } }
Output
Hello from Singleton!
Both references point to the same instance.
Common Pitfalls
Common mistakes when creating singletons include:
- Making the constructor
public, which allows multiple instances. - Not using
staticfor the instance variable or method. - Not handling multithreading, which can create multiple instances in concurrent environments.
For thread safety, use synchronized method or other concurrency controls.
java
public class WrongSingleton { public static WrongSingleton instance = new WrongSingleton(); public WrongSingleton() { // public constructor allows multiple instances } public static WrongSingleton getInstance() { return instance; } } // Correct way with thread safety public class ThreadSafeSingleton { private static ThreadSafeSingleton instance; private ThreadSafeSingleton() {} public static synchronized ThreadSafeSingleton getInstance() { if (instance == null) { instance = new ThreadSafeSingleton(); } return instance; } }
Quick Reference
- Make constructor
privateto prevent external instantiation. - Use a
private staticvariable to hold the single instance. - Provide a
public staticmethod to return the instance. - Consider thread safety if your program uses multiple threads.
Key Takeaways
A singleton class restricts instantiation to one object by using a private constructor and a static instance method.
Always keep the constructor private to prevent creating multiple objects.
Use a static method to provide global access to the single instance.
For multithreaded programs, ensure thread safety with synchronization or other concurrency controls.
Singletons are useful for shared resources like configuration or logging.