0

I am trying to understand the following code snippet that apparently is a lazy-loading example of the singleton attribute in the LazyHolder class.

class Singleton {
  // My private inner class containing my future lazy loading attribute
  private static class LazyHolder {
    private static Singleton singleton = new Singleton();
  }

  public static Singleton getSingleton() {
    return LazyHolder.singleton;
  }
}
class Main {
  public static main(String[] args) {
    Singleton s = Singleton.getSingleton();
  }
}

According to some online courses I have been following, the private static singleton instance will be created at the line:

Singleton s = Singleton.getSingleton()

But since the singleton attribute is static, I thought it's created during the LazyHolder class definition. So my question is:

When is the LazyHolder class really created?

  • is it created when the Singleton class is created, because LazyHolder is an attribute of Singleton class, or
  • is it created when we enter in the getSingleton() method of Singleton class?

I would really appreciate an explanation of what's happening during the runtime execution.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
idash
  • 29
  • 6
  • Don't write many questions in one post. Focus only on single problem that you want to get answer. – Roman C Apr 05 '23 at 15:30
  • First of all, hello and thank you for your response. By asking multiple questions, I am just trying to detail and share explicitly my way of thinking. Didnt you just see I said I was new to this ? By downgrading my post, you are just pulling me down and not up – idash Apr 05 '23 at 15:32
  • 1
    Since you are new here, I recommend you to read [How to ask a question](https://StackOverflow.com/help/how-to-ask). – Roman C Apr 05 '23 at 15:35
  • @Hulk thank you for your response. Not really, because I dont understand why the LazyHolder class would be created during the method call. In fact the java compiler should normally see that the LazyHolder class is used somewhere in the code, so the compiler, when building the byte codes, should put some instructions like :" load the LazyHolder class before starting the main execution" .. – idash Apr 05 '23 at 15:45
  • It's the opposite. The Java language specification requires that classes are not loaded until they are used at run time. So the Java compiler is expressly forbidden from "seeing that the LazyHolder class is used ... load(ing) the LazyHolder class before starting the main execution." The JVM is *required* to wait until the class is actually invoked or referenced in some way at run time, then the class is loaded. – markspace Apr 05 '23 at 15:52
  • @markspace oh okay I see now, I didnt know that thank you a lot ! Does it work the same for all compilers like in C/C++, ADA etc. ? – idash Apr 05 '23 at 15:57
  • 3
    No, most compilers are the opposite of Java. Check the language spec of whatever you are using. – markspace Apr 05 '23 at 16:13
  • The duplicate linked by Hulk does answer your question, see the topvoted answer: _"The JVM defers initializing the InstanceHolder class until it is actually used,"_ – Mark Rotteveel Apr 06 '23 at 10:37

0 Answers0