New Singleton class
Thread-safe and lazy loading.
This commit is contained in:
parent
84efa3039f
commit
cffe592c9c
18
singleton/src/main/java/com/iluwatar/SingletonClass.java
Normal file
18
singleton/src/main/java/com/iluwatar/SingletonClass.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class SingletonClass {
|
||||||
|
|
||||||
|
private static SingletonClass singletonInstance = null;
|
||||||
|
|
||||||
|
public synchronized static SingletonClass getSingleton() {
|
||||||
|
/*
|
||||||
|
* The instance gets created only when it is called for first time.
|
||||||
|
* Lazy-loading
|
||||||
|
*/
|
||||||
|
if (singletonInstance == null) {
|
||||||
|
singletonInstance = new SingletonClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
return singletonInstance;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user