I find two different ways to implement a singleton pattern, they are Lazy Initialization and Eager Initialization. and the code is:
public class EagerSingleton {
private static final EagerSingleton instance = new EagerSingleton();
private EagerSingleton() {
}
public static EagerSingleton getInstance() {
return instance;
}
}
… and:
public class LazySingleton {
private static LazySingleton instance;
private LazySingleton() {
}
public static synchronized LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
}
return instance;
}
}
It is said that the difference between those two ways is that EagerSingleton initializes early during class loading process, and LazySingleton initializes late when it is first used. But I can't see the timing difference because both them have private constructors, which means that you can't create an instance by new and let it without using (which can see the timing difference). The only way is to use the getInstance() method. So when you use this method, the class loading process and getInstance() both happen at this time. So it seems that they are not different.
Can someone explain the difference in this example?
Correct, the EagerSingleton class will instantiate the instance field during the class initialization.
Whereas, the LazySingleton class will wait for the getInstance method to be called.
You can utilize the class initialization block to evaluate the fields.
Consider the following.
Now, create 2 EagerSingleton and 2 LazySingleton objects.
Output