Checking if a class is java.lang.Enum

Solution 1:

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:

  • Class.isEnum()

Solution 2:

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.