3 Methods to Create Singleton with Java — Effective Java Notes
I only know one singleton creation method before, the factory method. The book list three methods and suggusts that the third method-Enum method is the best one. What a supurise :)
Method 1: Final static field
public class ElvisField {
public static final ElvisField INSTANCE = new ElvisField();
private ElvisField(){}
}The object is a static field.
Advantages: clear declarations
Disadvantages: Not serializable
Method 2: Factory method
I think this one is the most common one, I also use this one.
The method is also very clear: private constructor and a static getInstance() method to return the instance.
public class ElvisFactory {
public static ElvisFactory elvisFactory = null;
private ElvisFactory(){}
public static ElvisFactory getElvisFactoryInstance(){
if (elvisFactory==null){
elvisFactory = new ElvisFactory();
}
return elvisFactory;
}
}Advantage: Easily change the API if we don’t want to make it singleton.
Disadvantage: Not serializable. Extra effort needed to make it serializable.
To make it serializable, the following steps are needed:
- Implements the Serializable interface.
- Make all fields transient
- Add the readResolve method
public class ElvisFactorySerializable implements Serializable{
public static transient ElvisFactorySerializable instance = null;
private ElvisFactorySerializable(){}
public static ElvisFactorySerializable getInstance() {
if (instance==null){
instance = new ElvisFactorySerializable();
}
return instance;
}
private Object readResolve(){
return instance;
}
}Variables may be marked
transientto indicate that they are not part of the persistent state of an object.
In other words, transient field cannot be serielizable.
If we don’t do the transient and readResolve(), as the book stated. Otherwise, each time a serialized instance is deserialized, a new instance will be created, leading, in the case of our example, to spurious Elvis sightings.
Serialization is converting the objects to bytes, and the bytes could persist in storage or communication link. Usually, the objects are stored in memory, thus they are reclaimed after the running. But with the serialization, thos byes are still in the storage, then later we can deserialize(restore) the object from bytes which has the same state as before.
Method 3: Enum
public enum ElvisEnum {
INSTANCE
}Enum is the preferred method to create the Singleton.
Advantage: already has the serialization implementation.
I used to think that enum is only for list all available options, like enum color{RED, BLUE} . But actually, it can also be used like a normal class. The following code block is really similar to the static field method.
public enum ElvisEnum {
INSTANCE;
private int id;
private String name;
ElvisEnum(){
}
public int getId(){
return id;
}
public String getName(){
return name;
}}
We can also call the method.
ElvisEnum elvisEnum = ElvisEnum.INSTANCE;
elvisEnum.setId(12);
System.out.println(elvisEnum.getId());Reference:
- Effective Java
- StackOverflow Why does Java have transient fields?
- StackOverflow What is object serialization?
- StackOverflow What is the purpose of Serialization in Java?

