Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The Simplest Way to Define a Singleton in Java Correctly

DZone's Guide to

The Simplest Way to Define a Singleton in Java Correctly

Singleton patterns are often implemented incorrectly, so what's a simple way to not mess it up?

· Java Zone
Free Resource

Make it happen: rapid app development on Kubernetes as a managed service.

A singleton pattern is probably the simplest well-known design pattern, but still people often implement it incorrectly in Java. The main goal of the pattern is to ensure there is a single instance of a class that is globally accessible before it is first needed.

But wait, that could be accomplished just by a constant, right? Almost. But in reality, we often want to load the singleton lazily at the point we first need it, and we also want to ensure that it is not created multiple times in multi-threaded environments.

I’ve seen crazy complex solutions like this one to do it correctly. I mean, they are correct, but just too complex to implement by hand without a mistake. They are applicable in any language but they don’t take into account how the JVM works to simplify the solution.

My point is that JVM classloaders already provide both lazy loading and thread synchronization. Contrary to popular belief, like that all classes are loaded at start-up or when imported by another class, they are loaded when their bytecode is executed.

Therefore it is only necessary to define a Singleton class that includes the constant. In fact, Java enums are already suitable as they are defined within a separate class. The enum value will be only initialized once the class is loaded, and that would be when EnumSingleton.INSTANCE is first accessed. To make it work, it is only important that the enum contains only a single value holding the singleton.

public enum EnumSingleton{
    INSTANCE;

    private EnumSingleton() {
        /* constructor here */
    }

}

Note that this approach is not a new idea and it is also recommended by the Effective Java by J. Bloch. However, you often won't find out that this pattern really loads the singleton lazily, not even in the Bloch's book. There is no need to implement additional lazy loading and reinvent the wheel when the JVM does it for us and does it correctly even in a multithreaded environment.


For more details, check out my blog: The simplest way to define a singleton in Java correctly.

Tutorial: WordPress as distributed microservices on Kubernetes.

Topics:
singleton design pattern ,singleton ,singleton pattern ,java ,jvm

Published at DZone with permission of Ondrej Mihalyi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}