One of the methods I am currently writing for my Java library takes an array of an arbitrary enumeration type as its sole argument. If any of these is non-null, I can access the instance of java.lang.Class
representing that enumeration type, which may or may not be a public type. (If they are all null, there is no purpose to this anyway under the circumstances.) How do I get the number of possible values that enumeration type has? The approach I am currently using - Array.getLength(clazz.getMethod("values").invoke(null));
- fails when the Enum class is not public. How do I fix this?
Asked
Active
Viewed 632 times
1

SuperJedi224
- 131
- 1
- 8
-
1does this solve your problem: http://stackoverflow.com/questions/880365/any-way-to-invoke-a-private-method – Lynch Oct 27 '15 at 01:48
-
1@Lynch: Okay, that solved that problem. Now I'm running into a different issue, but I think I can straighten it out from here on my own. – SuperJedi224 Oct 27 '15 at 01:51
-
Can't you do `clazz.getEnumConstants().length`? – Paul Boddington Oct 27 '15 at 01:55
-
@PaulBoddington: I had forgotten about that method, thanks for reminding me. – SuperJedi224 Oct 27 '15 at 01:59
-
You also need to make sure you do `e.getDeclaringClass()` rather than `e.getClass()`... – Paul Boddington Oct 27 '15 at 02:00
-
Alright, thanks guys. – SuperJedi224 Oct 27 '15 at 02:13
-
@PaulBoddington: Wouldn't they be equivalent here? – SuperJedi224 Oct 27 '15 at 02:20
-
No, `e.getClass().getEnumConstants()` can return `null`. This is because if an enum has constant-specific method bodies, the constants need individual subclasses. – Paul Boddington Oct 27 '15 at 02:50
-
@PaulBoddington: Okay. Thanks. – SuperJedi224 Oct 27 '15 at 11:09
1 Answers
1
The easiest way to get an array of enum constants from a Class
object is
clazz.getEnumConstants();
To find the number of enum constants you can add .length
to this.
If you want to get the array of enum constants from an instance of an enum, it is important to do
e.getDeclaringClass().getEnumConstants();
rather than
e.getClass().getEnumConstants();
The reason for this is demonstrated by the following example:
private enum Colour {
WHITE,
BLUE {
@Override
public String toString() {
return "blue";
}
}
}
public static void main(String[] args) throws Exception {
System.out.println(Arrays.toString(Colour.BLUE.getClass().getEnumConstants()));
System.out.println(Arrays.toString(Colour.WHITE.getClass().getEnumConstants()));
System.out.println(Arrays.toString(Colour.BLUE.getDeclaringClass().getEnumConstants()));
}
This program outputs
null
[WHITE, blue]
[WHITE, blue]
What is going on here is that in order to override the method toString
for the BLUE
constant, a subclass of Colour
is created. This means that Colour.BLUE.getClass()
does not return Colour.class
, so Colour.BLUE.getClass().getEnumConstants()
returns null
. This issue does not apply for WHITE
because WHITE
does not require an extra class.

Paul Boddington
- 37,127
- 10
- 65
- 116
-
I know. I pointed it out. I thought it was better as an answer, – Paul Boddington Oct 27 '15 at 19:03