wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes enum values() method; enum's revisited Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "enum values() method; enum Watch "enum values() method; enum New topic
Author

enum values() method; enum's revisited

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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.


Tony Morris
Java Q&A (FAQ, Trivia)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16



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 ]

[My Blog]
All roads lead to JavaRanch
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum values() method; enum's revisited
 
Similar Threads
Recursion Problem
Sort array - by two fields
inner class or new class or new method?
Chap 1 ex 6 K&B (varargs)
arrayList iterator problems