I know that static variables are loaded at runtime, and although I thought I understand the issue, I had troubles with the next Eager Initialization Singelton implementation:
In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a singleton class but it has a drawback that instance is created even though client application might not be using it.
public class EagerInitializedSingleton {
private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();
//private constructor to avoid client applications to use constructor
private EagerInitializedSingleton(){}
public static EagerInitializedSingleton getInstance(){
return instance;
}
}
If the class's constructor is private
, how the class instance can be created at the time of class loading?
It has only one public
entry point, which the client has to call explicitly, right?