Implement Singlton

3 ways of writing Singleton.

using Enum

This is only available since Java 6.

public enum Singleton_Enum {
    INSTANCE;
}

using double checked locking

This is lazy loaded thread-safe Singleton, which is popular during Java 5 (with the use of Volatile variable).

public class Singleton_DoubleCheckedLocking implements Cloneable {
    private static volatile Singleton_DoubleCheckedLocking INSTANCE;

    private Singleton_DoubleCheckedLocking() {
    }

    public static Singleton_DoubleCheckedLocking getInstance() {
        if (INSTANCE == null) {
            synchronized (Singleton_DoubleCheckedLocking.class) {
                // double checking Singleton instance
                if (INSTANCE == null) {
                    INSTANCE = new Singleton_DoubleCheckedLocking();
                }
            }
        }
        return INSTANCE;
    }
}

using static factory method

Singleton instance is static and final variable it initialized when class is first loaded into memeory so creation of instance is inherently thread-safe.

public class Singleton_StaticFactory {
    // initailzed during class loading
    private static final Singleton_StaticFactory INSTANCE = new Singleton_StaticFactory();

    // to prevent creating another instance of Singleton
    private Singleton_StaticFactory() {
    }

    public static Singleton_StaticFactory getSingleton() {
        return INSTANCE;
    }
}

About thread-safety

Prior to Java 5 double checked locking mechanism is used to create thread-safe singleton in Java, which breaks if one Thread doesn’t see instance created by other thread at same time and eventually you will end up with more than one instance of Singleton class.

From Java 5 onwards volatile variable guarantee can be used to write thread safe singleton by using double checked locking pattern.

I personally don’t prefer that way as there are many other simpler alternatives like:

  1. using static field
  2. using Enum

Q & A

Question: How do you prevent for creating another instance of Singleton using clone() method?

Answer: Preferred way is not to implement Clonnable interface. And if you do, just throw Exception from clone() method as “Can not create clone of Singleton class”.