Last active
July 14, 2016 00:26
-
-
Save dannyduc/e71e85f5c1380bdf6cbd7c7afd4b2643 to your computer and use it in GitHub Desktop.
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 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