the instance of Singleton Class is created at the time of class loading. If we use this method, whather we use or not the object, object is created. it is not good. SO if we use the object at starting of the program use this approach. Let see how to implemnt this kind of singleton pattern
public class SingletonEager {
private static final SingletonEager instance = new SingletonEager();
//private constructor to avoid client applications to use constructor
private SingletonEager(){}
public static SingletonEager getInstance(){
return instance;
}
}
Here there is a private static variable it get initiated at the startup of the program. Becase static variables are created at the class loading
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.