Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scheffield/6510301796c68a01f2dd to your computer and use it in GitHub Desktop.
Save scheffield/6510301796c68a01f2dd to your computer and use it in GitHub Desktop.
/**
* LazySingleton#getInstance instatiate a singleton on the first request.
*
* Due to the loading behavior of static fields, this aproach is guaranteed to be thread safe.
*/
public final class LazySingleton {
private static class InstanceHolder {
public static final LazySingleton INSTANCE = new LazySingleton();
}
private LazySingleton() {
}
@Nonnull
public static LazySingleton getInstance() {
return InstanceHolder.INSTANCE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment