Skip to content

Instantly share code, notes, and snippets.

@dannyduc
Last active July 14, 2016 00:26
Show Gist options
  • Save dannyduc/e71e85f5c1380bdf6cbd7c7afd4b2643 to your computer and use it in GitHub Desktop.
Save dannyduc/e71e85f5c1380bdf6cbd7c7afd4b2643 to your computer and use it in GitHub Desktop.
public class Something {
private Something() {
System.out.println("Constructor called...");
}
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance() or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class LazyHolder {
private static final Something INSTANCE = new Something();
}
public static Something getInstance() {
return LazyHolder.INSTANCE;
}
public static String foo() {
return "bar";
}
public static void main(String[] args) {
System.out.println("started...");
System.out.println(Something.foo());
Something something = Something.getInstance();
System.out.println("end");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment