79

I'm trying to know if a class is an Enum, but I think I'm missing something:

if (test.MyEnum.class instanceof Enum<?>.class)
 obj = resultWrapper.getEnum(i, test.MyEnum.class);
else 
 obj = resultWrapper.getObject(i);

It gives me an error saying that Enum.class is not valid. So how I can check if a class is a Enum? I'm pretty sure it is possible to determine that, I'm just unable to get it.

Thanks

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Jose L Martinez-Avial
  • 2,191
  • 4
  • 28
  • 42

2 Answers2

163

The correct syntax would be:

Enum.class.isAssignableFrom(test.MyEnum.class)

but for enums, here is a more convenient method:

if (someObject.getClass().isEnum()))

Update: for enum items with a body (e. g. that override methods), this won't actually work. In that case, use

if (someObject instanceof Enum<?>)

Reference:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 7
    Beware of `isEnum()`. It returns false if your class is an anonymous class created by one of the enum values overriding a method. – Hakanai May 08 '14 at 03:42
  • @Trejkaz could you elaborate on that? I'm not quite following you – Sean Patrick Floyd May 12 '14 at 07:39
  • 6
    If you have methods in your enum, individual values of the enum can override them. If you override any of these methods it creates an anonymous class which is a subclass of the enum. isEnum() for that class returns false. Thus, even though EnumClass.class.isEnum() returns true, EnumClass.SOME_VALUE.getClass().isEnum() might return false, so you have to be careful about where you get the class from. For instance, if you were writing a utility method for others to call, using isEnum() would be a bad idea and using Enum.class.isAssignableFrom(clazz) would be safer. – Hakanai May 12 '14 at 23:24
  • 1
    @Trejkaz I see, in that case, getDeclaringClass() would be the correct way to do it. will update my answer. thanks! – Sean Patrick Floyd May 13 '14 at 08:37
  • In some cases someObject.getClass().isEnum() doesn't work as expected, for example what happens if enum implements an interface? (I am getting false).... – Paolo Jun 11 '15 at 12:00
  • @Paolo implementing interfaces doesn't make a difference as you can see here: https://ideone.com/2GrfJb , but it does make a difference whether the enum item has a body, in which case you have to consult the enclosing class (and not the declaring class as I have written above). So your only safe bet for all cases is `someObject instanceof Enum>` :-( – Sean Patrick Floyd Jun 12 '15 at 07:24
  • `someObject.getClass().getSuperclass() == java.lang.Enum.class` would also work and especially useful if you only have a Class: `someClass.getSuperclass() == java.lang.Enum.class` – Benny Bottema Sep 06 '18 at 18:04
  • @Benny Bottema "someObject.getClass().getSuperclass() == java.lang.Enum.class" is false if someObject is an enum constant with a body . It will then return you the class of the enum type itself, e.g. "TimeUnit.DAYS.getClass().getSuperclass() == java.util.concurrent.TimeUnit.class". You can use that to find out whether two enum constants with bodies are constants of the same enum. – mmirwaldt Feb 11 '21 at 22:34
14

If you're talking about Java 5 new feature - enum (it's not very new actually), then this is the way to go:

if (obj.getClass().isEnum()) {

...
}

If Enum is your custom class, then just check that obj instanceof Enum.

Roman
  • 64,384
  • 92
  • 238
  • 332