Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

The Singleton pattern in Java

Reference for the Java singleton pattern: eager, holder-idiom and enum singletons, double-checked locking with volatile, and why singletons hurt testing.

A singleton is a class that has exactly one instance in the running program, with a global way to reach it. Typical uses are configuration registries, caches and logging front ends. The pattern is simple to write but easy to get wrong when several threads race to create the instance.

Java gives you three dependable ways to do it: an eagerly created static field, the initialization-on-demand holder idiom, and a single-constant enum. Hand-written double-checked locking also works, but only with a volatile field.

Java
public final class Registry {
    private static final Registry INSTANCE = new Registry();
    private Registry() { }
    public static Registry getInstance() { return INSTANCE; }
}

Try it

Java Eager, holder and enum singletons

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Double-checked locking and many threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Variants

Variant Lazy? Thread-safe? Notes
Static final field No (at class load) Yes Simplest. Class initialization is guaranteed to run once.
Holder idiom Yes Yes The nested class loads only on first call to getInstance().
Enum with one constant No Yes Safe against reflection and deserialization creating a second instance. Cannot extend another class.
Double-checked locking Yes Only with volatile More code; usually not needed any more.
synchronized getter Yes Yes Correct but takes a lock on every call.

Usage notes

  • Make the constructor private and the class final, so nobody else can create or subclass it.
  • Without volatile, double-checked locking can publish a partly constructed object to another thread. Do not leave it out.
  • Serialization: a serializable singleton creates a new instance on deserialization unless you add readResolve() returning the instance. An enum avoids the problem.
  • Reflection can call a private constructor with setAccessible(true); again an enum is immune.
  • Class loaders: one JVM can hold several copies of a class (application servers, plugins), each with its own "singleton".
  • Testing is harder. Global state leaks between tests and cannot be replaced by a fake. Prefer creating one object at startup and passing it to its users (dependency injection); containers such as Spring offer singleton scope for that purpose.
  • Mutable singleton state needs the same synchronization as any shared data.
  • Hidden coupling: every getInstance() call is a dependency that is invisible in a constructor signature.