This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
After reading, and posting in the earlier thread about enum types, I decided to do some experimenting. According to the Sun all enum types extend java.lang.Enum, and therefore inherit all the methods of this class. However looking at the API for this class I don't see the values() method defined anywhere. As a matter of fact, I wouldn't even know it exised but for the Sun tutorial on enums. However this code definitely works:
So my question is, where is this method defined?
[edited because I spell like a third grader]
[ January 26, 2006: Message edited by: Garrett Rowe ] [ January 26, 2006: Message edited by: Garrett Rowe ]
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Sure they are. The relevant fact, however, is that the Enum class does not actually define a values() method. If it did, that method would have been inherited by every enum type. And it would have been pretty much useless, because it would not have been specific to any particular enum type. That doesn't meant that other classes can't inherit static methods - they can and do. But values() in particular would be useless as a static method defined in Enum.
[Tony]: The static values() method is inserted into the enum type at compile-time.
This part is true. Details can be found at JLS3 8.9. [ January 27, 2006: Message edited by: Jim Yingst ]