A Java singleton class is a class that allows only one instance to exist in the JVM (or, more precisely, per class loader) and gives the rest of the program a single access point to it. You create one by making the constructor private and exposing the instance through a static method or a single-element enum. The enum approach is the simplest safe choice in modern Java.
Singletons are used for shared, stateless or carefully synchronised services such as configuration holders, registries and caches. They are also one of the most overused patterns, so this article covers how to implement one correctly and when to prefer dependency injection instead.
Every correct singleton has three parts:
new.getInstance()), or an enum constant, that returns that instance.The main design question is when the instance is created (eagerly at class loading, or lazily on first use) and how to keep that creation thread-safe.
The instance is created when the class is loaded. The JVM guarantees class initialisation is thread-safe, so no extra locking is needed.
This is simple and safe. The downside is that the object is created even if nothing ever uses it, which matters only when construction is expensive.
The initialization-on-demand holder idiom creates the instance on first call to getInstance() without any explicit synchronisation. The nested class is not loaded until it is referenced, and class loading is thread-safe.
This is the recommended lazy approach for ordinary classes.
Double-checked locking also creates the instance lazily, but only works if the field is volatile. Without volatile, another thread can see a partially constructed object.
It works, but it is more code and easier to get wrong than the holder idiom. A plain synchronized method is also correct, just slower under heavy contention.
Joshua Bloch recommends a single-element enum as the best way to implement a singleton, because the JVM guarantees one instance and the enum is safe against both serialization and reflection attacks.
Usage:
An enum cannot extend another class (it can implement interfaces), and it is created eagerly when first referenced, so use the holder idiom if you need a base class.
A class-based singleton can be broken in several ways, and each has a fix.
| Threat | What happens | Fix |
|---|---|---|
Reflection (setAccessible(true) on the constructor) | A second instance is created | Throw an exception in the constructor if an instance already exists, or use an enum |
| Serialization | Deserializing creates a new object | Avoid Serializable, or add readResolve() returning the instance; enums handle this automatically |
| Cloning | clone() returns a copy | Do not implement Cloneable; make the class final |
| Multiple class loaders | One instance per loader, for example in application servers | Be aware of it; a singleton is per class loader, not per JVM |
A hand-written singleton is global mutable state. It hides dependencies, makes unit tests harder because you cannot swap in a fake, and couples code to a concrete class. In most applications a better option is to let a dependency injection container manage the lifecycle. In Spring, beans are singleton-scoped by default, so one instance per application context is shared without any singleton code in the class:
Use a hand-written singleton for small, genuinely global, effectively immutable things (a constants holder, a logger factory). Prefer injection for anything with behaviour you want to test or replace.
No. Creation can be made thread-safe with eager initialisation, the holder idiom, an enum or volatile double-checked locking. The methods and fields of the singleton still need their own synchronisation if they hold mutable state.
A single-element enum is the safest. If you need lazy creation or a superclass, use the holder idiom.
Not if it has a private constructor, and you should declare it final to make that intent clear.
A Spring singleton is one instance per application context, managed by the container. The pattern restricts the class itself to one instance per class loader, and nothing else can create it.
A singleton restricts a class to one instance with a private constructor and a static access point. Prefer a single-element enum, or the holder idiom for lazy creation, and use volatile if you write double-checked locking. Remember that reflection, serialization and multiple class loaders can break naive implementations, and consider dependency injection instead of global singletons in application code.