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
.
3 Answers 3
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 (onlyimplements
) - Some predefined methods (like
static values()
andgetName()
) are available - constructors can only be package private, or private
6 Comments
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!");
Comments
(Whether or not the singleton pattern is a good fit for your example use is discussed in other answers and comments. I will pretend to answer the specific question, even though it may not actually be relevant.)
Bloch's Effective Java has a nice discussion of this. See pp 17-18. I'm cribbing directly from this reference!
Basically, you can enforce the singleton pattern with an enum or a private constructor. Bloch prefers the former, even though it is a less common pattern.
e.g.:
// Enum singleton
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}
Functionality equivalent to a public field approach, and is ready for serialization without any more boilerplate. And there will only ever be a single instance, even in the face of tricksy reflection or serialization access.
Employee
simply doesn't feel like a natural singleton.