I know I can get the public static members of a class by doing something like:
obj.getClass().getFields()
but this doesn't get me the enums. I'd like to be able to get them from the Class object returned by the getClass method. Any ideas?
I know I can get the public static members of a class by doing something like:
obj.getClass().getFields()
but this doesn't get me the enums. I'd like to be able to get them from the Class object returned by the getClass method. Any ideas?
(Turned into a community wiki as it looks like there's scope for a fair amount of expansion, e.g. to include tackline's comments. No sense in me just transcribing comments when everyone could be expanding it.)
Do you mean enums nested within a top-level class? If so, use Class.getDeclaredClasses()
and iterate through the results seeing if any of the nested classes are enums. The simplest way of testing each nested class is to use Class.isEnum()
; if you want to iterate through the values within the enum then Class.getEnumConstants()
is the way to go.