Javatpoint Logo
Javatpoint Logo

Java Singleton Enum

In this article, we will be acknowledged about singleton Enum in Java. You will be able to understand how to use it, its purposes and most importantly its advantages and disadvantages.

Let's understand what is a singleton in programming.

Singleton

A class called a singleton is one that is meant to have just one instance for JVM. Multiple threads reuse the same singleton class object. Since system configurations as well as window administrators should be shared for all threads and objects under such a JVM, singletons are most frequently used to represent them.

Making singletons can be done using a number of common techniques.

  • With Public Static Final Field
  • With Public Static Factory Method
  • With Lazy Initialization

Using a private constructor, each of the aforementioned methods enforces non-insatiability (the inability to create instances). Even though we have nothing to perform inside of the private constructor in this case, we are unable to avoid generating one. Because if we do so, a default constructor with no parameters is implicitly generated and given the same access modifier as the class. For instance, the default constructor is public if the class is defined public; similarly, the default constructor is protected if the class is marked protected.

Singleton With Enum

The three lines mentioned above create a singleton that is free of all the issues mentioned. We don't need to construct it using a serializable interface because Enums are naturally serializable. Additionally, the reflection issue does not exist. Therefore, it is absolutely certain that a JVM will contain just one instance of a singleton. As a result, it is advised to use this technique when creating singletons in Java.

Way to Utilize

The above is the method or the function body that implements the singleton Enum. Next is followed by the main class.

One thing to keep in mind is that field variables really aren't serialized when serializing an Enum. For instance, the value of a int value field will be lost if the SingletonEnum class is serialized and deserialized.

Let us understand the advantages and disadvantages of Java Singleton Enum

Advantages

Writing Enum Singletons is simple

The biggest advantage of realizing that multiple instances are possible despite double-checked locking if you have been building singletons prior to Java 5 is without a doubt this. Even though this issue has been resolved with Java 5 and guarantees for volatile variables, many beginners still find it challenging to write.

Enum singletons are incredibly simple when comparing to double-checked encryption with synchronization. If you disagree that traditional singletons with double-checked locking as well as Enum singletons are comparable.

Java's singleton implementation of Enum is thread-safe by default, but any additional Enum methods must be written by the programmer.

Enum Singletons took care of Serialization on their own

Conventional singletons also have the issue that if you adopt a serializable interface, they cease to be singletons since the method readObject() automatically returns a new instance, much like the Java constructor. You can circumvent it by using Singleton in place of readResolve() and discarding recently founded instances, as demonstrated in the code below:

This can get even more complicated if your Singleton Class keeps state; you'll need to make them ephemeral, but the JVM ensures serialization with Enum Singleton.

Thread-safe creation of Enum instances

The Enum object is thread-safe by default, so you don't have to be concerned about double-checked locking.

In conclusion, with the guaranteed serialization and thread-safety, and with a few lines of code Enum, the Singleton approach is the ideal approach to create Singletons in the Java 5 world.

File name: Enum.java

Output:

0
2

Disadvantages

Coding Restrictions

There seem to be things that are allowed in Enum classes but not in normal classes. Using a constructor to access a static field, for instance. The developer needs to use greater caution considering that he is working at a higher level.

Serializability

Being discrete in nature is fairly typical for singletons. These singletons shouldn't typically be serializable. Transferring a domain specific singleton from one virtual machine to another makes no sense in practice; a singleton is defined as "unique within a VM," not "unique in the universe."

If a stateful singleton actually has to be serialized, it must define precisely and openly what it means to deserialize such singleton in a different virtual machine (VM), wherever there might be already a singleton of the same kind.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA