3

Is this correct:

  • Using a singleton with a holder gives lazy initialisation because the class SingletonHolder is only initialised when Singleton.getInstance() is run. This relies on SingletonHolder only ever being referenced inside Singleton.getInstance(). It's thread safe because the class loader takes care of synchronisation.
  • Using a singleton without the holder is eager initialisation because as soon as Java comes across code that references Singleton, all its static fields are resolved. It's also thread safe because the class loader takes care of synchronisation.

Singleton with a holder.

public class Singleton { private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } private Singleton(){ } } 

Singleton without a holder.

public class Singleton{ private static final Singleton INSTANCE = new Singleton(); public static Singleton getInstance(){ return INSTANCE; } private Singleton(){ } } 

Update in response to @jan's suggestion that this is a duplicate of What is an efficient way to implement a singleton pattern in Java?. I disagree. I am not asking what is the best way to do it: I am only asking what makes these two specific implementations lazy vs eager loading. Answers like xyz's broadly address lazy vs eager, but not by contrasting the two examples I was trying to examine (or with the same keywords which is why it never came up in my initial searches).

4
  • There are multiple ways to implement singleton, lazy/eager depends on your implementation. stackoverflow.com/a/16580366/705773 might help. Commented Dec 29, 2015 at 7:33
  • Possible duplicate of What is an efficient way to implement a singleton pattern in Java? Commented Dec 29, 2015 at 7:50
  • 1
    You can verify this using a static{} block and printing out something to see when the actual fields are initialized. Commented Dec 29, 2015 at 9:01
  • "Using a singleton without the holder is eager initialisation because as soon as Java comes across code that references Singleton, all its static fields are resolved." This is simply not true. If you refer to this code by import statement for example, statics are not resolved. Commented Nov 5, 2021 at 6:45

1 Answer 1

4

In response to @Sriram, here is my test to prove which is eager vs lazy loading.

Lazy loading with a holder

public class Singleton { private static class SingletonHolder { static { System.out.println("In SingletonHolder static block."); } private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { System.out.println("In getInstance()."); return SingletonHolder.INSTANCE; } private Singleton() { System.out.println("In constructor."); } private void doSomething() { System.out.println("Singleton working."); } public static void main(String[] args) { System.out.println("Start of main."); Singleton.getInstance().doSomething(); System.out.println("End of main."); } } 

The output shows that the main method starts before getInstance() is called, thus lazily loaded.

Start of main. In getInstance(). In SingletonHolder static block. In constructor. Singleton working. End of main. 

Eager loading without a holder

public class Singleton { static { System.out.println("In Singleton static block."); } private static final Singleton INSTANCE = new Singleton(); public static Singleton getInstance() { System.out.println("In getInstance()."); return INSTANCE; } private Singleton() { System.out.println("In constructor."); } private void doSomething() { System.out.println("Singleton working."); } public static void main(String[] args) { System.out.println("Start of main."); Singleton.getInstance().doSomething(); System.out.println("End of main."); } } 

The output shows that the main method starts after the getInstance() method is called, thus eagerly loaded.

In Singleton static block. In constructor. Start of main. In getInstance(). Singleton working. End of main. 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.