# Initialization-on-demand Holder Idiom Also known as the **Bill Pugh Singleton**. Generally considered the best way to implement a thread-safe Singleton in Java — no explicit synchronization needed. ## How It Works ```java public class Singleton { private Singleton() {} private static class Holder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return Holder.INSTANCE; } } ``` ## Why It Works Two JLS guarantees: 1. **Lazy class loading** — `Holder` is not loaded until `getInstance()` is first called 2. **Thread-safe class initialization** — the JVM guarantees only one thread triggers loading; all others wait ## vs Double-Checked Locking | Holder Idiom | Double-Checked Locking | |---|---| | No `synchronized` or `volatile` | Requires both | | Cannot be broken by reordering | Broken without `volatile` (pre-Java 5) | | Simpler, less error-prone | Subtle correctness requirements | | Guaranteed by JLS (class loading) | Relies on subtle JMM semantics (`volatile`) | ## Limitations - **Java-specific** — relies on JVM class loading semantics - **No constructor arguments** — instance created without parameters - **No error recovery** — if constructor throws, class enters failed state (`NoClassDefFoundError`) --- See also: [[Double-Checked Locking]], [[Compare-and-Swap]]