A singleton class in Java is a class that allows only one instance per JVM class loader and gives the rest of the program one global access point to it. In modern Java the safest ways to write one are an enum singleton, a private constructor with a static final field, or the initialization-on-demand holder idiom.
The pattern is useful for things that must be shared, such as a configuration registry or a metrics collector. It is also easy to overuse, because a singleton is global state and makes code harder to test, so consider dependency injection first.
Every Java singleton follows the same three rules:
private constructor, so no other class can call new.private static field that holds the single instance.public static method (or an enum constant) that returns it.The differences between implementations are when the instance is created (eagerly or lazily), whether creation is thread safe, and whether reflection, serialization or cloning can create a second instance.
The instance is created when the class is initialized. The JVM guarantees that class initialization is thread safe, so no locking is needed.
This is the simplest correct option. Its only drawback is that the object is built even if nobody uses it, which matters only when construction is expensive.
The initialization-on-demand holder idiom (often called the Bill Pugh singleton) creates the instance the first time getInstance() is called, with no explicit synchronization. The nested class is loaded only when it is first referenced.
For most applications this is the best lazy option.
Marking getInstance() as synchronized is correct but takes a lock on every call.
Double-checked locking avoids the lock after creation, but the field must be volatile. Without volatile, another thread can see a partly constructed object because of instruction reordering.
Prefer the holder idiom over this unless you have a reason to control creation yourself. A plain lazy getInstance() with no synchronization is not thread safe: two threads can both see null and create two objects.
Joshua Bloch recommends a single-element enum in Effective Java. The JVM guarantees one instance, and the enum also resists reflection and serialization attacks.
An enum cannot extend another class, though it can implement interfaces. That is the one limitation.
| Attack | How it creates a second instance | Defence |
|---|---|---|
| Reflection | Constructor.setAccessible(true) then newInstance() | Throw from the constructor if the instance exists, or use an enum |
| Serialization | Deserializing creates a new object | Implement readResolve() returning the instance, or use an enum |
| Cloning | clone() on a Cloneable class | Do not implement Cloneable |
| Multiple class loaders | Each loader has its own static state | Accept it, or use a container-managed singleton |
On modern JDKs with the module system, strong encapsulation also limits reflective access to JDK internals, but it does not stop reflection on your own classes in the unnamed module.
A hand-written singleton hides a dependency: any class can call Config.getInstance(), which makes unit tests hard to isolate. In frameworks such as Spring, beans are singleton-scoped by default, so the container creates one instance and injects it where needed. You get the single-instance behaviour with constructor injection and without static access.
Use a manual singleton only for stateless utilities or truly process-wide resources, and keep any mutable state inside it thread safe.
For most cases use an enum singleton or the holder idiom. Eager initialization with a private static final field is also correct and the simplest.
No. Only eager, holder and enum designs are thread safe during creation. A lazy if (instance == null) check needs synchronization, and the singleton's own mutable state needs its own thread safety.
Without volatile, the Java memory model allows another thread to read a non-null reference before the constructor has finished, so it may see a half-built object.
Not always, but they are global state and complicate testing. Prefer dependency injection and let a container manage the single instance.
A Java singleton has a private constructor, a static instance and a global accessor. Choose an enum or the holder idiom for safe, simple code, add volatile if you use double-checked locking, and guard against reflection and deserialization when it matters. When you can, let a dependency injection container own the single instance instead.