Java Singleton Pattern
To implement Singleton pattern, we have different approaches but all of them have following common concepts.
/**
* Singleton class.
*/
public final class Singleton {
/**
* Private constructor so nobody can instantiate the class.
*/
private Singleton() {}
/**
* Static to class instance of the class.
*/
private static final Singleton INSTANCE = new Singleton();
/**
* To be called by user to obtain instance of the class.
*
* @return instance of the singleton.
*/
public static Singleton getInstance() {
return INSTANCE;
}
}