What are the downsides of implementing a singleton with Java's enum?
Posted
by
irreputable
on Programmers
See other posts from Programmers
or by irreputable
Published on 2012-12-14T00:39:56Z
Indexed on
2012/12/14
5:18 UTC
Read the original article
Hit count: 360
Traditionally, a singleton is usually implemented as
public class Foo1
{
private static final Foo1 INSTANCE = new Foo1();
public static Foo1 getInstance(){ return INSTANCE; }
private Foo1(){}
public void doo(){ ... }
}
With Java's enum, we can implement a singleton as
public enum Foo2
{
INSTANCE;
public void doo(){ ... }
}
As awesome as the 2nd version is, are there any downsides to it?
(I gave it some thoughts and I'll answer my own question; hopefully you have better answers)
© Programmers or respective owner