Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save rayjcwu/9267081 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9267081 to your computer and use it in GitHub Desktop.
public class SingletonMain {
public static void main(String []argv) {
EnumSingleton es = EnumSingleton.INSTANCE;
ClassSingleton cs = ClassSingleton.getInstance();
}
}
enum EnumSingleton {
INSTANCE;
public void exec() {}
}
class ClassSingleton {
private static ClassSingleton INSTANCE = null;
private ClassSingleton(){}
public static ClassSingleton getInstance() {
if (INSTANCE == null) {
synchronized (ClassSingleton.class) {
if (INSTANCE == null) {
INSTANCE = new ClassSingleton();
}
}
}
return INSTANCE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment