Java Singletons using enum type (The best method for making Singletons in Java)

Dulaj Atapattu
4 min readJul 5, 2017

--

Singleton is a class that suppose to have only one instance per JVM. Same instance of the singleton class is reused by multiple threads. Most often we use singletons to represent system configurations, window managers like resource since those instances should be common to all threads and objects within a JVM.

Traditional methods of making singletons

There are several popular methods of making singletons.

Method 1: Singleton with public static final field

Method 2: Singleton with public static factory method

Method 3: Singleton with lazy initialization

Pros and Cons of above methods

All of the above methods enforce the noninstantiability (unable to make instances) using a private constructor. Here we cannot avoid creating the private constructor even though we have nothing to do inside the constructor. Because if we do so, then an implicit parameterless default constructor is created having the same access modifier as the class. For example, if the class is declared public, then the default constructor is public; if the class is declared protected, then the default constructor is protected (Refer oracle docs for more details).

Comparing above methods, first 2 methods don’t have a performance difference at all. Method 1 is more clearer and simple. A minor advantage of method 2 is, later you can make the class non singleton without changing the API. You can do it by changing the implementation of the factory method to create a new instance for every call instead of returning the same instance as follows.

public static Singleton getInstance() {    return new Singleton ();}

Static fields are initialized at the class loading time. Therefore in both method 1 and 2, singleton instances are created even in a case we don’t use them at the run time. This is not a problem as long as the Singleton object is not too big and creating the instance is not much expensive. Method 3 avoids this problem with lazy initialization. In method 3 the instance is created when we access the singleton object for the first time. Fine grained synchronization is used to ensure that no more than 1 objects are created with multiple concurrent threads.

All of the above methods works fine until you are not doing serialization and deserialization with singleton class. Let’s think again how did we achieve the singleton behavior in above methods. It was done by making the constructor private and making inaccessible the constructor to create new instances of the class. But the problem is, actually isn’t there any other ways to create an instance of a class other than the constructor? Answer is no. There are some other advance methods.

  1. Serialization and deserialization
  2. Reflection

Problems with serialization and deserialization

In order to serialize above singleton classes we must have to implement those classes with Serializable interface. But doing that is not enough. When deserializing a class new instances are created. Now it doesn’t matter the constructor is private or not. Now there can be more than one instance of the same singleton class inside the JVM violateing the singleton property.

Outputs for the above code is,

Two objects are not same
2
1

Here singleton and singleton2 are two different instances. This violates the singleton property. The solution is, we have to implement the readResolve method which is called when preparing the deserialized object before returning it to the caller. The solution is as follows.

Now the output for above code will be,

Two objects are same
2
2

Now the singleton property is preserved and only one instance of the singleton class exists within the JVM.

Problems with Reflection

An advance user can change the private access modifier of the constructor to anything he want at run time using reflection. If this happens, our only mechanism for noninstantiability breaks. Let’s see how this can be done.

Output of the above code,

Two objects are not same
1
2

In this way, non accessible private constructor becomes accessible and the whole idea of making the class singleton breaks.

Making Singletons with enum in Java

All of the above problems can be solved very easily by using enum type for making singletons.

Singleton with enum

Just above 3 lines makes a singleton without any of the problems discussed above. Since enums are inherently serializable we don’t need to implement it with serializable interface. Reflection problem is also not there. Therefore, it is 100% guaranteed that only one instance of the singleton is present within a JVM. Thus, this method is recommended as the best method of making singletons in java.

How to use

One thing to remember here is when serializing an enum, field variables are not get serialized. For example, if we serialize and deserialize above SingletonEnum class, we will loss the value of the int value field (Refer oracle docs for more details about enum serialization).

All the source codes related to this post can be found at: https://github.com/dulajra/all-about-singletons

Article originally posted at: http://blog.dulaj.info/2017/07/java-singletons-using-enum-type-best.html

--

--

Dulaj Atapattu

Senior Software Engineer | Full Stack Developer | DevOps Engineer

Recommended from Medium

Lists

See more recommendations