Why does javac add values()
and valueOf(String)
methods to the enum type being defined? Wouldn't it have been better that they were added to Enum class itself?
What I mean is, if I have some enum such as
enum FooEnum {ONE, TWO}
javac adds values()
and valueOf(String)
to FooEnum
when compiling it. I find it a bit odd. What is the reason behind this?
Is it only to ensure type safety of the returned value/values or is there anything else? And if it's for type safety alone, wouldn't Generics have helped?