Is it possible to get the enumeration values from a Class? Let me elaborate this a bit. If I have an enum for example FooBar, then I can get the values of FooBar by calling FooBar.values() which will return an array with the enumerations (FooBar[]). My problem is, that I have a method which takes as an input parameter a class (Class c) and within the method I check whether or not it is an enum (c.isEnum()). If it is an enum, I would need to somehow get the enum values, the problem is that I don't know quite how to do this, I'm not even totally sure it is possible. So, is it possible and if it is, how can I accomplish this? (Note that the solution needs to be generic, in other words not if-else's).
Yes, a couple of times and I somehow just kept missing that method o_O
Kim L
2009-06-09 06:39:25
Just to add an example how to use it: http://java.sun.com/docs/books/tutorial/reflect/special/enumMembers.html
Kosi2801
2009-06-09 06:40:12
A:
Edited: You can use c.getClass().getEnumConstants() to get the values of the constants
(it said 'names' instead of 'values' and added some unnecessary stuff)
Confusion
2009-06-09 06:41:51
Wrong. getEnumConstants() returns the values, not the names - and they're even typed through generics if the class object is, though that probably does not apply in this case.
Michael Borgwardt
2009-06-09 06:49:48
Ah, I thought I just tested that, but there's a bug in my quick'n dirty test; sorry about that.
Confusion
2009-06-09 07:03:52