Created
May 4, 2011 20:04
-
-
Save codeswimmer/955922 to your computer and use it in GitHub Desktop.
Java: Singleton - Lazy Loaded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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