Skip to content

Lazy Initialization

Singleton class can be instantiated by two methods:

Early / Eager initialization

  • In this method, class is initialized whether it is to be used or not.
  • The main advantage of this method is its simplicity.
  • Its drawback is that class is always initialized whether it is being used or not.
  • Early initialization is just reverse, you initialize a singleton upfront at the time of class loading.

Implmentation (Early)

Step 1

Create a Singleton Class.
SingleObject.java

public class SingleObject {
   //create an object of SingleObject
   private static SingleObject instance = new SingleObject();

   //make the constructor private so that this class cannot be
   //instantiated
   private SingleObject(){}

   //Get the only object available
   public static SingleObject getInstance(){
      return instance;
   }

   public void showMessage(){
      System.out.println("Hello World!");
   }
}

Step 2

Get the only object from the singleton class.
SingletonPatternDemo.java

public class SingletonPatternDemo {
   public static void main(String[] args) {
      //illegal construct
      //Compile Time Error: The constructor SingleObject() is not visible
      //SingleObject object = new SingleObject();

      //Get the only object available
      SingleObject object = SingleObject.getInstance();

      //show the message
      object.showMessage();
   }
}

Lazy initialization

  • In this method, class in initialized only when it is required.
  • It can save you from instantiating the class when you don’t need it.
  • Generally, lazy initialization is used when we create a singleton class.
  • Lazy initialization means that you do not initialize objects until the first time they are used.

Implmentation (Lazy)

SingletonPatternDemo.java

class LazySingleObject {

    // create static object and declare it
    // the object is declared not initialized
    // it is called lazy initialization as initializaiton is done when needed, not early
    private static LazySingleObject object;

    // Make the constructor private so that the object cannot be instantiated.
    // It can only be accessed through getInstance()
    private LazySingleObject(){};

    // method to initialized and return the object of the class
    public static LazySingleObject getInstance() {
        if (object == null) {
            // if object is null, initialize
            object = new LazySingleObject();
        }
        return object;
    }

    public void displayMessage(){
        System.out.println("Object is instantiated");
    }
}


public class LazySingletonPattern {
    public static void main(String[] args) {

        // since the constructor is private, the statement below gives error
        // LazySingleObject object = new LazySingleObject();

        // getInstance() is static, so it doesn't need any object to be initialized for it to be called
        LazySingleObject object = LazySingleObject.getInstance();
        object.displayMessage();
    } 
}