I implement Singleton using the enum approach:
public enum Singleton { INSTANCE; public void doStuff(String stuff) { System.out.println("Doing " + stuff); } }
How can I name it correctly (I do not mean execution, how to write it)? Is this still class or is it an enumeration now? I'm just interested in the theoretical part.If we talk about the orientation of objects and classes, can I say that I created the Singleton class to enumerate types?
Enumeration in Java is just a class (as you seem to know), so you should call it any of the above. This is a bit subjective, but I think it depends on what aspects you want to highlight.
, - .
, . Enum - .
INSTANCE, ( INSTANCE). Jvm , ; , .
. java , enum enum ( , , JVM), Singleton.INSTANCE .
:
public class Singleton { public static Singleton instance INSTANCE = new Singleton(); private Singleton() { /* private ctor so people can't instantiate their own */ } }
enum ( JDK5 )
Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum.
Enum, Java5.0.
Singleton Enum.
learn more about Enum