i have the following what you might call a lazy loaded singleton per the definition:
public class MySingleton {
public static String myTrigger="a trigger";
private MySingleton(){
}
private static enum LazyLoad {
IMDB_LOOKUP_INSTANCE;
private static final IMDB_LOOKUP = new MySingleton();
}
public static MySingleton getInstance() {
return LazyLoad.IMDB_LOOKUP_INSTANCE.IMDB_LOOKUP;
}
}
What happens when i make a call like this:
String someString = MySingleton.myTrigger;
will the singleton not get instantiated ?
After testing (By putting a print statement in the constructor) , I found that -
In the above code, the instantiation will not occur untill the call to MySingleton.getInstance()
But if you put the static MySingleton object as a direct property of the class, instead of inside the enum , it will get instantiated on the call to MySingleton.myTrigger , this is because all static fields are instantiated when the class is loaded.
But in case of enum, enum is not a static property of the class, it is only loaded on access.
I tested something like this as well -
In the above , the call to
MySingleton.LazyLoad.IMDB_LOOKUP_INSTANCE.Hello
would also cause instantiation of MySingleton object.