Was playing with some generics and java's enums earlier and can't figure out why the code below gives me a compile error about the 'values()' method call.
It's saying that :-
the method values() is undefined for the type E
But I thought since a java enum implicitly extends Enum and values() is a static method given by using an enum type, there's no reason the below wouldn't work (as I've stated that E is extending Enum). Anybody have any suggestions or have any reason why it's giving me that error?
Note : Interestingly I did take a look at the Java API (1.6) and it looks like Enum doesn't actually have the values() method specified??? So if this is the case where does a java enum get the values() method from (so should my extends be extending that?) or is it some real trickery done by the compiler (thus the code below cannot be fixed?). I did notice that if I use any of the methods from the Enum Api declarations, they compile)
Any help would be much appreciated.
Ps. Before anybody comments, I know the below code isn't really logical since I could use valueOf(...) to do the same but its meant as an illustrative example of what I'm really doing so please don't comment on that and only post if you have something about my actual question.
Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. For example, this code from the Planet class example below iterates over all the planets in the solar system.
so, maybe you're right. Maybe, this method does not exist BEFORE compiling time.