Skip to content

Instantly share code, notes, and snippets.

@AlexanderBrevigSublime
Created October 1, 2012 09:51
Show Gist options
  • Save AlexanderBrevigSublime/3810624 to your computer and use it in GitHub Desktop.
Save AlexanderBrevigSublime/3810624 to your computer and use it in GitHub Desktop.
Java: Thread safe singleton
class YourSingletonClass {
public static synchronized YourSingletonClass getInstance() {
if (instance == null) {
instance = new YourSingletonClass();
}
return instance;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
private YourSingletonClass() {
// construct your class
}
private static YourSingletonClass instance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment