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
.
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.
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.
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.
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.
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:
extend
another class (only implements
)static values()
and getName()
) are availableYour "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!");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With