
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make a singleton enum in Java?
In Java, an enum is a special class used to represent a group of constants (that cannot be changed, like a final variable), such as Days: SUNDAY, MONDAY, TUESDAY, etc.
Following is the syntax to create an Enum class in Java:
enum ClassName { VALUE1, VALUE2, VALUE3, // ... VALUEN; }
Where, enum is a reserved keyword in Java used to define an enum class, ClassName is the name of the enum, and VALUE1, VALUE2, VALUE3,..., VALUEN are the constant values that the enum groups together.
What is a Singleton Enum in Java?
A singleton enum in Java is a type-safe and concise way to implement the singleton design pattern, which ensures that a class has only one ( or single) instance throughout the application.
Enum-based singletons are considered the best practice for implementing singleton design patterns in Java. To make a class (not an enum) in Java a singleton, you can use the following syntax:
public class className{ private className(){ // private constructor to prevent instantiation from outside } }
The constructor is declared as private to prevent instantiation from outside the class.
Making a Singleton Enum
In Java, to make an enum a Singleton, we need to define an enum with a single element (usually called INSTANCE). This method is considered the most useful and thread-safe way to implement singleton design patterns in Java.
Following is the syntax of creating a singleton enum in Java:
public enum Singleton { INSTANCE; //single instance of the class private singleton() { //private constructor to prevent instantiation from outside } }
Where, INSTANCE is the single instance of the enum that represents the singleton object, and the private specifiers specify that the class can not be instantiated outside.
Example
The following example shows how to create and use the singleton enum in Java:
public enum EnumSingleton { //single instance of enum class INSTANCE; private String name; private int age; private void build(SingletonBuilder builder) { this.name = builder.name; this.age = builder.age; } // static getter public static EnumSingleton getSingleton() { return INSTANCE; } //method to print name and age public void print() { System.out.println("Name: "+name + ", age: "+age); } public static class SingletonBuilder { private final String name; private int age; private SingletonBuilder() { name = null; } public SingletonBuilder(String name) { this.name = name; } public SingletonBuilder age(int age) { this.age = age; return this; } public void build() { //using the single instance of enum EnumSingleton.INSTANCE.build(this); } } public static void main(String[] args) { new SingletonBuilder("Rohan").age(22).build(); EnumSingleton.getSingleton().print(); } }
The above program produces the following output:
Name: Rohan, age: 25
Why use a Singleton Enum in Java?
There are a few reasons why we can use an enum as a singleton in Java as follows:
- Guaranteed one instance (Cannot instantiate more than one enum even through reflection).
- Thread-safe.
- Serialization.