Singleton Pattern in Java: Best Practices

March 11, 2014

Creational patterns such as Singleton provide mechanisms for object instantiation. The purpose of the Singleton is to ensure that only one instance of a particular class is created and provide global access to it.

There are many opinionated views about the approaches of  implementing Singleton among developers in terms of efficiency and java memory model. The way I see it there are 4 main approaches of implementing this pattern in java which are both efficient and thread-safe.

1. Early/eager initialization with a static factory method
2. Lazy initialization with double-checked locking
3. Initialization on demand holder
4. Enum singleton