What is the purpose of the enum class introduced in Java 5?

My suggestion:

1) The two enumerations only exist before compilation (like generics; but I've never heard anything about it, and what it writes anywhere is to delete generics after compilation)

2) Or enum is a way to maintain backward compatibility in some way (but I haven't seen it yet)

Any other suggestions? (by the way, did you use it in your code?)

UPD: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html

In other words, there is an enum keyword and an enum class They all appear in Java 5 The question is: why do we need both?

Second question: Why did enum become part of the API?

Solution

Start with the Java tutorials:

Enumeration is very useful. Yes, I have used it many times in the code

Edit:

... has an enum keyword and an enum class They all appear in Java 5 The question is: why do we need both?

This question is similar to asking "there is a class keyword and an object class. Why do we need two?"

In both cases, the keyword is basically the prompt of the compiler; You can think of them as syntax candy to save you keystrokes and let the compiler reduce its workload (by not guessing what you want to declare)

See this Q & A

Have you ever used it in code?

The answer is still "yes" In particular, enum#valueof() is a static method for parsing strings:

DayOfWeek d = Enum.valueOf(DayOfWeek.class,"TUESDAY");

But of course it also works:

DayOfWeek d = DayOfWeek.valueOf("TUESDAY");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>