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?

shrini1000
  • 7,038
  • 12
  • 59
  • 99

2 Answers2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Pl. see the comment above. Also, 'Class' defines an 'enumConstantDirectory' instance variable. So couldn't it be reused for these other two methods? – shrini1000 Oct 03 '12 at 07:47
  • 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 Oct 03 '12 at 07:55
  • 1
    @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. – Jon Skeet Oct 03 '12 at 07:58
  • 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. – shrini1000 Oct 03 '12 at 07:59
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)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • The Enum class already has a method 'valueOf(Class enumType, String name)'. So couldn't these two other methods also have been defined that way, possibly returning T[] and T? – shrini1000 Oct 03 '12 at 07:45
  • 2
    `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. – Peter Lawrey Oct 03 '12 at 07:51