This example shows creating a Lazy singleton. Means the singleton instance
creation is delayed until it is really required. This approach helps to
improve application startup performance.
/**
* Make sure to have private default constructor.
* This avoids direct instantiation of class using
* new keyword/Class.newInstance() method
*/ private LazySingleton() {
init();
}
private void init() {
//Initialization code
}
/**
* Make the 'getInstance' method as 'synchronized method'
* to avoid two thread creating multiple instances
* of LazySingleton.
*
* @return
*/ public static synchronized LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
}