Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created May 4, 2011 20:04
Show Gist options
  • Select an option

  • Save codeswimmer/955922 to your computer and use it in GitHub Desktop.

Select an option

Save codeswimmer/955922 to your computer and use it in GitHub Desktop.
Java: Singleton - Lazy Loaded
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() {}
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment