Last active
March 12, 2016 08:41
-
-
Save zzlalani/2b30f35344f78b4730c3 to your computer and use it in GitHub Desktop.
Singleton example
This file contains 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
class Singleton { | |
private static Singleton instance; | |
private Singleton() { | |
} | |
/** | |
* Reference: http://stackoverflow.com/tags/singleton/info | |
*/ | |
public static Singleton getinstance() { | |
if (instance == null) { | |
synchronized (Singleton.class){ | |
if (instance == null) { | |
instance = new Singleton(); | |
} | |
} | |
} | |
return instance; | |
} | |
public void print() { | |
System.out.println("Singleton Class!"); | |
} | |
} |
This file contains 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
class SingletonTest { | |
public static void main(String[] args) { | |
Singleton singleton = Singleton.getinstance(); | |
singleton.print(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment