6

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?

2 Answers 2

6

They're static methods - how could they have been added to Enum? Static methods aren't polymorphic. Additionally, what would the implementation have looked like in Enum, in your proposed scheme? Enum itself doesn't know the values - only the concrete type does.

Sign up to request clarification or add additional context in comments.

4 Comments

Pl. see the comment above. Also, 'Class' defines an 'enumConstantDirectory' instance variable. So couldn't it be reused for these other two methods?
E.g., define an instance array field in Enum and use it to store subclass Enums. Of course then the corresponding methods will need to be instance level, but sub classes now can reuse that super class code.
@shrini1000: "Of course then the corresponding methods will need to be instance level" - which would defeat the whole point, surely. If I'm trying to get an instance, I may not have an instance. It's not logically a per-instance operation.
Ok, I get it. The methods have to be static because they are util methods, and we can't have a static array because then it will be shared by all subclasses, hardly what we want.
6

Why does javac add values() and valueOf(String) methods to the enum type being defined?

This keeps the class self contained.

Wouldn't it have been better that they were added to Enum class itself?

The compiler can't do this as the Enum class has already been compiled and you might not be using the same copy in any case.

This could be done at runtime but you add complexity e.g. it makes unloading the class more difficult for little gain.

What is the reason behind this?

There is no better place to put it.

Note: Enum.valueOf(clazz, name) calls clazz.valueOf(name); as it wouldn't make any sense to call a clazz.valueOf(clazz, name); (though you can if you want to confuse people)

2 Comments

The Enum class already has a method 'valueOf(Class<T> enumType, String name)'. So couldn't these two other methods also have been defined that way, possibly returning T[] and T?
Enum.valueOf(clazz, name) calls clazz.valueOf(name); so they are defined the same removing the redundant parameter. As the first method is static it cannot be overridden so its not possible to change its behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.