Skip to content

Instantly share code, notes, and snippets.

@zzlalani
Last active March 12, 2016 08:41
Show Gist options
  • Save zzlalani/2b30f35344f78b4730c3 to your computer and use it in GitHub Desktop.
Save zzlalani/2b30f35344f78b4730c3 to your computer and use it in GitHub Desktop.
Singleton example
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!");
}
}
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