Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum Foo {
- INSTANCE;
- }
- public enum Elvis {
- INSTANCE;
- private final String[] favoriteSongs =
- { "Hound Dog", "Heartbreak Hotel" };
- public void printFavorites() {
- System.out.println(Arrays.toString(favoriteSongs));
- }
- }
- public enum Foo {
- INSTANCE;
- }
- public final class Foo {
- private static final Foo INSTANCE = new Foo();
- private Foo() {
- if (INSTANCE != null) {
- throw new IllegalStateException("Already instantiated");
- }
- }
- public static Foo getInstance() {
- return INSTANCE;
- }
- }
- public final class Foo {
- private static class FooLoader {
- private static final Foo INSTANCE = new Foo();
- }
- private Foo() {
- if (FooLoader.INSTANCE != null) {
- throw new IllegalStateException("Already instantiated");
- }
- }
- public static Foo getInstance() {
- return FooLoader.INSTANCE;
- }
- }
- public final class Foo implements Serializable {
- private static final long serialVersionUID = 1L;
- private static class FooLoader {
- private static final Foo INSTANCE = new Foo();
- }
- private Foo() {
- if (FooLoader.INSTANCE != null) {
- throw new IllegalStateException("Already instantiated");
- }
- }
- public static Foo getInstance() {
- return FooLoader.INSTANCE;
- }
- @SuppressWarnings("unused")
- private Foo readResolve() {
- return FooLoader.INSTANCE;
- }
- }
- class Foo {
- private static class BarHolder {
- public static Bar bar = new Bar();
- }
- public static Bar getBar() {
- return BarHolder.bar;
- }
- }
- class Foo {
- private static volatile Bar bar = null;
- public static Bar getBar() {
- if (bar == null) {
- synchronized(Foo.class) {
- if (bar == null)
- bar = new Bar();
- }
- }
- return bar;
- }
- }
- public class A {
- private static A singleton = new A();
- private A() {}
- public static A getInstance() {
- return singleton;
- }
- }
- public class Singleton {
- private final static Singleton INSTANCE = new Singleton();
- private Singleton() {}
- public static Singleton getInstance() { return Singleton.INSTANCE; }
- protected Object clone() {
- throw new CloneNotSupportedException();
- }
- }
- public class Singleton {
- private static Singleton instance = null;
- private Singleton() {}
- public static Singleton getInstance() {
- if(null == instance) {
- synchronized(Singleton.class) {
- if(null == instance) {
- instance = new Singleton();
- }
- }
- }
- return instance;
- }
- protected Object clone() {
- throw new CloneNotSupportedException();
- }
- }
- public static final INSTANCE == ....
- private Object readResolve() {
- return INSTANCE; // original singleton instance.
- }
- public class Singleton {
- private static Singleton instance = null;
- static {
- instance = new Singleton();
- // do some of your instantiation stuff here
- }
- private Singleton() {
- if(instance!=null) {
- throw new ErrorYouWant("Singleton double-instantiation, should never happen!");
- }
- }
- public static getSingleton() {
- return instance;
- }
- }
Add Comment
Please, Sign In to add comment