Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enum values() method; enum's revisited

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

According to the Sun all enum types extend java.lang.Enum, and therefore inherit all the methods of this class.

Static methods are not inherited. The static values() method is inserted into the enum type at compile-time.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


About "Hidden" static methods:
http://dev2dev.bea.com/pub/a/2005/09/java_5_features.html?page=2
[ January 26, 2006: Message edited by: Satou kurinosuke ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Tony]: Static methods are not inherited.

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic