What is the use of enumeration without an instance?

I read and implemented the script through enum. I realized that we can create an enumeration without any instance. What is the practical use of such an Enum ? Secondly, Enum can also implement an interface, but obviously cannot extend the class, since it already extends the Enum class. What are the practical benefits of creating Enum without instances? Ben

+8
java
source share
3 answers

Zero-member enumerations are actually a utility-class idiom used by a particular segment of the Java community (primarily Peter Lawrey ). They are the most concise and possibly the cleanest way to ensure that a class cannot be created or subclassed.

Naturally, you will not have any instance methods in such an enumeration; only static.

+10
source share

Enum are reference types such as class or interfaces . This means that you can adapt the principle well, the program for the interface, and not for implementation . "

+2
source share

enum members are not necessarily committed to the end of the universe. You can add new members without breaking binary compatibility. Therefore, I can imagine that enum is being created to model some concept that has no instances at present, but can be added later.

Is it possible to create an enum for a concept that is known to never have any instances? For example enum NegativeNaturalNumber{} ? Can it serve any purpose in the program? Who knows, we cannot say with certainty.

0
source share

All Articles