Quick revision
The short answer
Eager static initialisation is the simplest and is fine unless construction is costly or may fail. The holder-class idiom gives lazy, thread-safe initialisation with no synchronization on later reads, but only works for static fields. Volatile double-checked locking works for both static and instance fields, and later reads cost one volatile read; for per-instance lazy fields, copying the field into a local variable avoids extra volatile reads. Java 25 also has a preview API for lazily set constant values, Stable Values, so check its status before relying on it.
What to remember
In this answer, we compared the ways to initialise singletons and lazy fields. Eager static initialisation is simplest and fine unless construction is costly or may fail. The holder-class idiom gives lazy, lock-free static singletons but only for static fields, and both make a failed construction permanent. Volatile double-checked locking works for static and instance fields, allows retry after failure, and costs one volatile read per call, which a local variable copy keeps to one. Java 25's Stable Values preview may offer a better option, but check its status first.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Eager static initialisation is the simplest and is fine unless construction is costly or may fail. The holder-class idiom gives lazy, thread-safe initialisation with no synchronization on later reads, but only works for static fields. Volatile double-checked locking works for both static and instance fields, and later reads cost one volatile read; for per-instance lazy fields, copying the field into a local variable avoids extra volatile reads. Java 25 also has a preview API for lazily set constant values, Stable Values, so check its status before relying on it.
Why this question is asked
Once we know that plain double-checked locking is broken, the practical question is which correct option to use where. The choice matters more than it seems: a singleton that fails during eager initialisation can make a whole class unusable, a lazy field read on every request should not pay more than necessary, and a per-instance lazy field cannot use the holder idiom at all. Interviewers ask this to see whether we can match the technique to the situation and explain the cost of each. In this answer, we will go through eager initialisation, the holder idiom and volatile double-checked locking in turn, then look at lazy per-instance fields, and finish with the newer Stable Values API.
1Eager Static Initialisation
The simplest option is to create the instance in a static final field. The JVM runs static initialisers once, under a lock, when the class is first used, and publishes the result safely to every thread.
Notice that this is already somewhat lazy: a class is initialised on first active use, such as calling a static method, not when the program starts. So eager initialisation only creates the object earlier than needed if the class has other static members used before get(). It is fine for most singletons.
There are two cases where it is not. If construction is costly, such as loading a large file or opening connections, and the instance may never be needed, we pay for nothing. And if construction may fail, the exception happens during class initialisation: the first caller gets an ExceptionInInitializerError, and every later use of the class throws NoClassDefFoundError, with no way to retry. That is a hard failure mode for anything that depends on external resources.
2The Holder-Class Idiom
The holder idiom moves the instance into a nested class that is initialised only when get() is first called.
The JVM's class initialisation gives us laziness, exactly-once creation and safe publication, and once Holder is initialised, reading INSTANCE is an ordinary static final read that the JIT can treat as a constant. So later reads cost nothing. The limitation is that it only works for static state, because class initialisation happens once per class, not once per object. And it shares the failure behaviour of eager initialisation: if the constructor throws, Holder fails to initialise and later calls throw NoClassDefFoundError, so it suits construction that will not fail, or failure that should be fatal.
3Volatile Double-Checked Locking
Double-checked locking with a volatile field is the general tool. It works for static fields and for instance fields, and a failed construction simply leaves the field null so a later call can try again.
| Option | Lazy | Later reads | Fields | If construction fails |
|---|---|---|---|---|
| Eager static final | At class init | Plain, constant-foldable | Static only | Class unusable |
| Holder-class idiom | On first get() | Plain, constant-foldable | Static only | Holder unusable |
| Volatile DCL | On first get() | One volatile read | Static or instance | Field stays null, retry |
After creation, each call costs one volatile read. On x86 that is about the same as a plain read at the hardware level, though the JIT cannot treat it as a constant or hoist it out of loops. On ARM, a volatile read uses a load-acquire instruction, which costs a little more. For most code this is negligible, but it is the reason the holder idiom is preferred for static singletons.
4Lazy Per-Instance Fields
Sometimes each object needs its own lazily computed value, such as a parsed form of a large text, computed on first use and then reused. The holder idiom cannot do this, since it is per class. Volatile double-checked locking can, using the object itself or a private lock.
The local variable p matters here. Without it, the fast path reads parsed twice, once for the check and once for the return, and each is a volatile read. With it, the fast path reads the field once. That is the standard form of the idiom, and it is the one to use in code review.
If computing the value twice is harmless, because it is cheap and idempotent, a simpler racy-but-safe variant drops the lock: read the volatile into a local, compute if null, and write it back, accepting that two threads may both compute. This is the single-check idiom, and it is only correct if the value itself is immutable or safely constructed.
5Stable Values: A Newer Option
Java 25 includes a preview API, Stable Values, for values that are set at most once and then treated by the JVM as constants. It aims to give the laziness of double-checked locking with the read performance of a static final field, for both static and instance fields.
Because it is a preview feature, it needs --enable-preview, and its API and name can change between releases; it has been revised in later JDK releases, so check its status in the JDK you run before relying on it. For production code on Java 25 today, the holder idiom and volatile double-checked locking remain the standard choices.
6Follow-up questions
When would you pick eager initialisation over the holder idiom?
When construction is cheap and cannot fail, which covers most singletons. The holder adds a nested class for laziness we often do not need, since class initialisation already happens on first use. I would choose the holder when construction is expensive and the class has other static members that might be used without needing the instance.
How do these compare with simply making get() synchronized?
A synchronized get() is correct and simple, but every call acquires the lock. Uncontended locks are cheap, so it is fine for rarely called code, but on a hot path read by many threads it adds contention. The three options above all make later reads lock-free.
7Conclusion
In this answer, we compared the ways to initialise singletons and lazy fields. Eager static initialisation is simplest and fine unless construction is costly or may fail. The holder-class idiom gives lazy, lock-free static singletons but only for static fields, and both make a failed construction permanent. Volatile double-checked locking works for static and instance fields, allows retry after failure, and costs one volatile read per call, which a local variable copy keeps to one. Java 25's Stable Values preview may offer a better option, but check its status first.
Open with
I would choose based on three things: whether the field is static, whether construction is expensive, and whether it can fail. Eager static initialisation, a static final field, is the simplest and the JVM makes it thread-safe.
Then, if they want more
It is also lazier than people think, since class initialisation happens on first active use. It is a poor fit if construction is costly and may not be needed, or if it can fail, because a failed static initialiser gives ExceptionInInitializerError and then NoClassDefFoundError forever.
The holder-class idiom puts the instance in a nested static class, so it is created on the first get call, exactly once, by the JVM's class initialisation, and later reads are plain static final reads with no synchronization. That is my default for lazy static singletons, but it only works for static fields and shares the permanent-failure behaviour.
Volatile double-checked locking works for static and instance fields, and if construction throws, the field stays null so a later call can retry. Each later read costs one volatile read, which is cheap on x86 and a little more on ARM, but cannot be constant-folded.
For per-instance lazy fields, it is the main option, and I always copy the field into a local variable so the fast path does one volatile read instead of two. If recomputation is harmless, the single-check variant without a lock also works for immutable values.
Finally, Java 25 has Stable Values as a preview API for lazily set values the JVM can treat as constants, but it needs enable-preview and has been revised since, so I would check its status before using it.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare volatile double-checked locking, the holder-class idiom and eager initialisation...": https://scrivio-interview-library.netlify.app/java/java-memory-model/67de902d45/