Enumeration or class?

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?

+3
source share
6 answers

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.

  • , , ,
  • ,
  • , singleton, enum
  • , singleton ( ), singleton
  • , , enum, singleton enum

, - .

+3

, . Enum - .

+2

INSTANCE, ( INSTANCE). Jvm , ; , .

0

. 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 )

0
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. 
0

Enum, Java5.0.

Singleton Enum.

learn more about Enum

0
source

All Articles