Created
November 29, 2016 10:30
-
-
Save kevalpatel2106/8d488553d2e8f9bf4e858e26344e3c7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SingletonClass { | |
private static SingletonClass sSoleInstance; | |
//private constructor. | |
private SingletonClass(){ | |
//Prevent form the reflection api. | |
if (sSoleInstance != null){ | |
throw new RuntimeException("Use getInstance() method to get the single instance of this class."); | |
} | |
} | |
public static SingletonClass getInstance() { | |
//Double check locking pattern | |
if (sSoleInstance == null) { //Check for the first time | |
synchronized (SingletonClass.class) { //Check for the second time. | |
//if there is no instance available... create new one | |
if (sSoleInstance == null) sSoleInstance = new SingletonClass(); | |
} | |
} | |
return sSoleInstance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment