My suggestions:
1) either enums exist only before compilation (like generics; but I’ve never heard anything about it whilst it’s written everywhere that generics are being erased after compilation)
2) or Enum is a way to maintain backward compatibility in some way (but I don’t see that way yet).
Any other suggestions? (btw, have you ever used it in your code?)
UPD: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html
In other words, there’s a enum keyword and Enum class. They both appeared in Java 5. The question: why do we need both?
The second question: why Enum became a part of the API?
From the Java Tutorials:
Enums are extremely useful, and yes, I have used them plenty of times in my code.
Edit:
…there’s a enum keyword and Enum class. They both appeared in Java 5. The question: why do we need both?
This question is analogous to asking “there’s a
classkeyword and anObjectclass. Why do we need both?”In both cases, the keywords are basically hints to the compiler; you could think of them as syntactic sugar that saves you keystrokes, and lets the compiler do less work (by not making it guess about what you’re declaring).
See this q&a as well.
…have you ever used it in your code?
The answer is still “yes.” In particular,
Enum#valueOf()is a static method useful for parsing strings:but of course this works, too: