Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

Compare volatile double-checked locking, the holder-class idiom and eager initialisation for lazy singletons in terms of cost and use cases. What about lazily initialised per-instance fields?

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.

Java
public final class Clock {
private static final Clock INSTANCE = new Clock(); // created when Clock is initialised
public static Clock get() { return INSTANCE; } // plain read, no synchronization
}

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.

Java
public final class Templates {
private Templates() { /* parse 500 templates, about 200 ms */ }
private static final class Holder {
static final Templates INSTANCE = new Templates();
}
public static Templates get() { return Holder.INSTANCE; } // first call: init; later: plain read
}

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.

Java
public final class Connectors {
private static volatile Connector instance;
public static Connector get() {
Connector c = instance; // 1 volatile read on the fast path
if (c == null) {
synchronized (Connectors.class) {
c = instance;
if (c == null) {
instance = c = Connector.open(); // if this throws, instance stays null: retry later
}
}
}
return c;
}
}
Comparing the three options
OptionLazyLater readsFieldsIf construction fails
Eager static finalAt class initPlain, constant-foldableStatic onlyClass unusable
Holder-class idiomOn first get()Plain, constant-foldableStatic onlyHolder unusable
Volatile DCLOn first get()One volatile readStatic or instanceField 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.

Java
final class Document {
private final String raw;
private volatile Parsed parsed; // lazily computed, per instance
Document(String raw) { this.raw = raw; }
Parsed parsed() {
Parsed p = parsed; // read the volatile once
if (p != null) return p; // fast path: 1 volatile read in total
synchronized (this) {
p = parsed;
if (p == null) {
parsed = p = Parser.parse(raw);
}
return p;
}
}
}

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.

Java
// Java 25 preview: compile and run with --enable-preview; check your JDK, the API may change
final class Document {
private final StableValue<Parsed> parsed = StableValue.of();
Parsed parsed() {
return parsed.orElseSet(() -> Parser.parse(raw)); // computed once, then constant-like
}
private final String raw;
Document(String raw) { this.raw = raw; }
}

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.

What to say in the interviewAbout 110 seconds aloud

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/