Java Singleton enum
tl;dr
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 {
INSTANCE;
// class body
// no need for private constructor
// no need for getInstance, just use Foo.INSTANCE
}
And that's it.
Written by Eran Medan
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Java
Authors
Related Tags
#java
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#