The Singleton design pattern in Java

In Java, sometimes it can be useful to have exactly one instance of a class. This is known as a Singleton pattern. A logger, print spooler, or window manager would be potential uses for this.

In essence, a Singleton ensures that a class has a single instance, and a global point of access to it. The class declaration typically looks like this:

public class mySingleton {
   private static mySingleton instance = null;
   protected mySingleton() {
      // Protected constructor to prevent instantiation.
   public static mySingleton getInstance() {
      if(instance == null) {
         instance = new mySingleton();
      return instance;

For a much more in-depth guide to this design pattern, check out

Written on October 18, 2014