Java Singleton Class
In Java, sometimes you want only one instance of a class throughout your application—for example, a configuration manager, logger, or database connection pool.
This is where the Singleton Design Pattern comes in. A Singleton Class ensures that:
- Only one instance of the class exists.
- It provides a global point of access to that instance.
In this guide, we’ll cover:
- What a singleton class is
- Different ways to implement it
- Advantages and disadvantages
- Best practices
What Is a Singleton Class?
A singleton class is a class that allows only one object to be created. Any further attempts to create a new object return the same instance.
Implementation of Singleton in Java
1. Eager Initialization
The instance is created at class loading time.
public class EagerSingleton {
private static final EagerSingleton instance = new EagerSingleton();
// private constructor prevents instantiation
private EagerSingleton() { }
public static EagerSingleton getInstance() {
return instance;
}
}
Simple to implement
✘ Instance created even if not used
2. Lazy Initialization
The instance is created only when needed.
public class LazySingleton {
private static LazySingleton instance;
private LazySingleton() { }
public static LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
}
return instance;
}
}
Saves memory (created on demand)
✘ Not thread-safe in multithreaded environments
4. Bill Pugh Singleton (Recommended)
Uses an inner static helper class for lazy loading + thread safety.
public class BillPughSingleton {
private BillPughSingleton() { }
// inner static helper class
private static class Holder {
private static final BillPughSingleton INSTANCE = new BillPughSingleton();
}
public static BillPughSingleton getInstance() {
return Holder.INSTANCE;
}
}
Lazy initialization
Thread-safe
High performance
5. Enum Singleton (Best Approach)
Since Java enums are thread-safe and prevent reflection/serialization attacks, they are considered the safest way to implement a singleton.
public enum EnumSingleton {
INSTANCE;
public void doSomething() {
System.out.println(“Singleton using Enum”);
}
}
Usage:
public class Main {
public static void main(String[] args) {
EnumSingleton singleton = EnumSingleton.INSTANCE;
singleton.doSomething();
}
}
Simplest and most robust implementation
Advantages of Singleton
- Controls access to a single instance
- Saves memory (single object reused)
- Useful for global states like logging, caching, and configurations
Disadvantages of Singleton
Can introduce global state, which makes testing harder
Increases tight coupling
Overused in some cases, leading to poor design
Best Practices
- Prefer Bill Pugh Singleton or Enum Singleton for thread safety.
- Use singletons only when truly needed.
- Avoid using them for data storage or business logic (can create hidden dependencies).
Conclusion
The Singleton Design Pattern ensures that only one instance of a class exists in your Java application.
- Use Eager Singleton for simple cases.
- Use Lazy + Thread-safe for multi-threaded apps.
- Use Bill Pugh or Enum Singleton for modern, robust implementations.
By using singletons wisely, you can write Java applications that are efficient, consistent, and maintainable.