Java: enumerating values () and valueof (string)

Why does javac add the value () and valueof (string) methods to the enumeration type being defined? Wouldn't it be better to add them to the enum class itself?

I mean, if I have some enumerations like this

enum FooEnum {ONE,TWO}

Javac adds values () and valueof (string) to fooenum at compile time I find it a little strange What is the reason behind this?

Is it just to ensure the type safety of the return value / value, or is there anything else? Wouldn't generics help if it was just for type safety?

Solution

They are static methods – how do you add them to enum? Static methods are not polymorphic In addition, how will enum be implemented in your proposed scheme? Enum itself does not know value - only specific types

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
分享
二维码
< <上一篇
下一篇>>