What is a singleton class and how to create one in Java?

Copied from http://www.javaworld.com/article/2073352/core-java/simply-singleton.html

When you need a class which is accessed by disparate objects throughout a software system, and therefore require a global point of access.

  • Ensure that only one instance of a class is created
  • Provide a global point of access to the object.

Thread Safe Singletons in Java: static synchronized methods.

public class ClassicSingleton {
   private static ClassicSingleton instance = null;
   protected ClassicSingleton() {
      // Exists only to defeat instantiation.
   }
   public static ClassicSingleton getInstance() {
      if(instance == null) {
         instance = new ClassicSingleton();
      }
      return instance;
   }
}