Design Patterns For Beginner (Part 1)

One of the basic design pattern which everyone uses is Singleton design Patterns

public class Singleton {
private static Singleton singleton = null;

  //making constructor private
  private Singleton() {
    }
  
  public static Singleton getInstance() {
        if (singleton == null) {
            synchronized(Singleton.class) {
                singleton = new Singleton();
            }
        }
        return singleton;
    }
}

Why does the constructor is private ?
Since constructor is now private,we have forced to make use of getInstance() to create and get the object.

Why is the singleton variable static?
The ‘singleton’ field holds the reference of the one and only instance.
It is stored in a static variable because its scope has to be the class itself and not a particular instance.