Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a singleton class using enum

I am trying to create a singleton class in Java. The best available solution with Java5 and above versions seems to be using enum. But I am not sure how to convert my class into a singleton class using enum. Following is my simplified class:

public class Employee { 
   private int id; 
   private String name; 
   public Employee() {} 
   public int getId() {
      return id; 
   }
   public void setId( int id ) { 
      this.id = id; 
   } 
   public String getName() { 
      return name; 
   } 
   public void setName( String name ) {
      this.name = name; 
   }
}

When I searched for answers in the net I found the following code:

public enum EasySingleton{
   INSTANCE;
}

But where are my class variables and methods? I am not sure how to implement this. I know we can provide methods to enum but where will my variables go? Any help on this would be really appreciated.

P.S.: Please don't debate if singleton are evil or anti-pattern. I am just curious on how to create a singleton using enum.

like image 668
Pradeep S Avatar asked Jun 19 '15 14:06

Pradeep S


People also ask

Can enum be used as singleton?

Enum Singletons are new ways of using Enum with only one instance to implement the Singleton pattern in Java. While there has been a Singleton pattern in Java for a long time, Enum Singletons are a comparatively recent term and in use since the implementation of Enum as a keyword and function from Java 5 onwards.

How would you implement singleton with enum?

The singleton pattern restricts the instantiation of a class to one object. INSTANCE is a public static final field that represents the enum instance. We can get the instance of the class with the EnumSingleton. INSTANCE but it is better to encapsulate it in a getter in case if we want to change the implementation.

Why enum is best for singleton?

Pros and cons It is simple to write Enum Singletons. Enums are inherently serializable. No problems of reflection occur. It is thread-safe to create enum instances.

Can you create a singleton class?

In Java, Singleton is a design pattern that ensures that a class can only have one object. To create a singleton class, a class must implement the following properties: Create a private constructor of the class to restrict object creation outside of the class.


2 Answers

The differences between a class and an enum are not so big. I changed the first line of code to public enum instead of public class and added the name of your instance.

public enum Employee { // changed "class" to "enum"

   INSTANCE; // added name of the (single) instance

   private int id; 
   private String name; 
   Employee() {} // removed "public"
   public int getId() {
      return id; 
   }
   public void setId( int id ) { 
      this.id = id; 
   } 
   public String getName() { 
      return name; 
   } 
   public void setName( String name ) {
      this.name = name; 
   }
}

Please keep in mind, that singeltons, enum instances, static things might hinder you later on, if you want to run your code several times in one vm. Consider creating an instance of Employee in your main class and pass it through your application.

Beside that, enums have some other special features:

  • You cannot extend another class (only implements)
  • Some predefined methods (like static values() and getName()) are available
  • constructors can only be package private, or private
like image 111
slartidan Avatar answered Oct 23 '22 12:10

slartidan


Your "Employee" class isn't really something that should be a singleton, but here goes.

public enum Employee { 
   INSTANCE;

   private int id; 
   private String name; 

   private Employee() {}  //enum constructor must be private

   public int getId() {
      return id; 
   }

   public void setId( int id ) { 
      this.id = id; 
   } 

   public String getName() { 
      return name; 
   } 

   public void setName( String name ) {
      this.name = name; 
   }
}

Then you can do

Employee.INSTANCE.setName("Hello World!");
like image 39
EpicPandaForce Avatar answered Oct 23 '22 11:10

EpicPandaForce