1

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 ?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
j2emanue
  • 60,549
  • 65
  • 286
  • 456

3 Answers3

1

There are issues with your enum. So, i have modified it and the following code works and initializes MySingleton.

public class MySingleton {

public static String myTrigger="a trigger";

private MySingleton(){
    System.out.println("Initialized");
}

private static enum LazyLoad {

    IMDB_LOOKUP_INSTANCE(new MySingleton());

    MySingleton value;

    LazyLoad(MySingleton value){
        this.value = value;
    }

    private MySingleton getValue(){
        return this.value;
    }

}

public static MySingleton getInstance() {
    return LazyLoad.IMDB_LOOKUP_INSTANCE.getValue();
}
}

Class gets loaded when you call MySingleton.myTrigger. But if you want your MySingleton to get initialized on class loading, put MySingleton.getInstance() in static block.

Ouney
  • 1,164
  • 1
  • 10
  • 22
0

Your singleton will not get instantiated until you call MySigleton.getInstance().

fo_x86
  • 2,583
  • 1
  • 30
  • 41
0

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 -

class MySingleton {

public static String myTrigger="a trigger";

    private MySingleton(){
        System.out.println("Printing");
    }

    public static enum LazyLoad {
        IMDB_LOOKUP_INSTANCE;
        public static final String Hello = "Hello";
        private static final MySingleton IMDB_LOOKUP = new MySingleton();
    }

    public static MySingleton getInstance() {
        return LazyLoad.IMDB_LOOKUP_INSTANCE.IMDB_LOOKUP;
    }
}

In the above , the call to MySingleton.LazyLoad.IMDB_LOOKUP_INSTANCE.Hello would also cause instantiation of MySingleton object.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • I tested it out, I am guessing it may be because when enum structure is loaded into memory, it is initializing all its static variables, but unless the call to the enum specifically, the enum structure is not getting loaded into memory. – Anand S Kumar Jun 10 '15 at 05:00
  • `MySingleton.LazyLoad.IMDB_LOOKUP_INSTANCE.Hello` is a static final variable, its not a constant, its still a variable , even though static and final. Its value is not inlined. `IMDB_LOOKUP_INSTANCE` is a constant. Also Enums are basically classes in java - http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – Anand S Kumar Jun 10 '15 at 05:13
  • I have tested it out, that particular call `MySingleton.LazyLoad.IMDB_LOOKUP_INSTANCE.Hello` does instantiate the object of MySingleton class, you can test it out yourself, you can check that call to `MySingleton.LazyLoad.IMDB_LOOKUP_INSTANCE.Hello` leads to printing of `Printing` , which happens when the MySingleton class is instantiated. – Anand S Kumar Jun 10 '15 at 05:22
  • Or not. [_The Primary expression is evaluated, and the result is discarded._](http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.11) That evaluation forces the initialization of `LazyLoad`. (Eclipse seems to handle this differently. That seems like a bug.) – Sotirios Delimanolis Jun 10 '15 at 05:47