Fundamentals 8 min read

Master Java Singleton: DCL, Volatile, Static Inner Class & Enum Explained

This article explores several advanced Java singleton implementations—including double‑check locking with volatile, static inner‑class lazy loading, and enum‑based singletons—explaining their mechanisms, thread‑safety benefits, and trade‑offs for modern backend development.

Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
Master Java Singleton: DCL, Volatile, Static Inner Class & Enum Explained

The singleton pattern ensures a class has only one globally shared instance, a concept familiar to most developers.

1. Double‑Check Locking (DCL)

DCL synchronizes each call to getSingleton() to guarantee thread safety. The key line uses volatile to prevent instruction reordering: private volatile static Singleton singleton; Without volatile, the compiler may reorder instructions and the CPU may execute them out of order on multi‑core systems, leading to a partially constructed object being visible to other threads. The typical failure sequence is:

Thread A begins instantiation but the reference is set before the constructor finishes.

Thread B sees the non‑null reference and returns the partially built object.

Subsequent method calls on that object cause NullPointerException or other errors.

2. Role of volatile

Since JDK 1.6, volatile ensures that writes to the variable are immediately visible to other threads, effectively providing a memory‑visibility guarantee that prevents the above race condition.

3. Static Inner Class

This approach leverages the class‑loader mechanism: the inner holder class is not loaded until getSingleton() is invoked, providing lazy initialization without explicit synchronization. It reduces code size and complexity, and the JVM guarantees that only one thread initializes the holder class. However, it does not protect against reflection or deserialization attacks, so it is not a strict singleton.

4. Enum Singleton

Joshua Bloch recommends using an enum to implement a singleton. The JVM ensures thread safety, and the enum prevents both reflection and deserialization from creating additional instances.

Is the Singleton Pattern Really Worth It?

Advantages:

Provides controlled access to a unique instance.

Saves system resources and can improve performance.

Disadvantages:

Lacks an abstraction layer, making extension difficult.

Often violates the Single Responsibility Principle.

In Spring‑based web projects, the framework’s singleton scope differs from the classic design‑pattern singleton: Spring creates one bean per IoC container, not per JVM. Spring also supports lazy initialization and custom scopes, giving developers flexibility. Thread safety for beans is generally the developer’s responsibility, with Spring using mechanisms like ThreadLocal for DAO layers. Source: Huawei Cloud Community, author “技术火炬手”.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaconcurrencyenumdesign patternvolatileSingletonDCL
Huawei Cloud Developer Alliance
Written by

Huawei Cloud Developer Alliance

The Huawei Cloud Developer Alliance creates a tech sharing platform for developers and partners, gathering Huawei Cloud product knowledge, event updates, expert talks, and more. Together we continuously innovate to build the cloud foundation of an intelligent world.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.