Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get enumeration values from Class

Tags:

java

enums

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).

like image 335
Kim L Avatar asked Jun 09 '09 06:06

Kim L


1 Answers

Um... have you looked at the API doc?

Class.getEnumConstants()

like image 199
Michael Borgwardt Avatar answered Oct 26 '22 16:10

Michael Borgwardt