Enum | Learn for Master
  • Five ways to implement Singleton pattern in Java

    In this post, I will describe five ways to implement the Singleton pattern in Java. They are Synchronization the getInstane() method,static final variable, using double checking lock with volatile keyword, using SingletonHolder, and Enum.

    1. Classic Java Singleton pattern

    The following code is the simplest implementation of Singleton Pattern in Java. However it is not multiple thread-safe. 

    The singleton pattern implemented in the above example is easy to understand. We maintain a static reference to the singleton instance and returns that reference from the static getInstance() method. We also make the Constructor private,

    [Read More...]