0

A comment to this question : A 'for' loop to iterate over an enum in Java states :

@jacktrades: It is an implicit method that exists only in the compiler. Therefore the base class can not declare a method with the same name and thus it does not get included in the automatically generated Javadocs. docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2 – Torben Mar 5 at 9:32

Since this method only exists in the compiler does this mean it existed before the Enum object becames available in jdk1.5 ? Does this method exist for other objects that are compiler only. java.util.Map contains a values() method but this is documented and so is explicit ? Why define this method .values as implicit, this is a useful method on Enum types so it should be explicit and available on the Enum object type ?

Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752

1 Answers1

2

Since this method only exists in the compiler does this mean it existed before the Enum object becames available in jdk1.5 ?

No, it was added for Java 5.

Does this method exist for other objects that are compiler only. java.util.Map contains a values() method but this is documented and so is explicit ?

That's different. Map.values() is a normal method, part of the Map interface. For any enum type, values() is a static method. The key difference is that static methods are not inherited, so adding values() to java.lang.Enum would not have helped. Another implicit method is [EnumType].valueOf(String name).

Why define this method .values as implicit, this is a useful method on Enum types so it should be explicit ?

Again, there's no way it could be explicit, since static methods are not inherited. For valueOf there's an analog valueOf(class<T> type, name) method in java.lang.Enum. I'm not sure why there is no values(class<T> type) in there; it would've made sense to me. But instead there is Class.getEnumConstants(), which does the same thing.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53