Last Updated: February 25, 2016
· eranation

Java Singleton enum


if you have a Java class that has one of the old anti patterns of a singleton (first ask yourself why you need a singleton, but that's a different story)

all you need to do is from this

public class Foo {

  private Foo() {


  public static Foo getInstance() {
    // whatever method you used before

  //class body

to this

public enum Foo {

  // class body

  // no need for private constructor
  // no need for getInstance, just use Foo.INSTANCE


And that's it.