Checking the java.lang.Enum class

I am trying to find out if the class is Enum, but I think something is missing:

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

This gives me an error saying that Enum.class is not valid. So how can I check if the class is Enum? I am sure that this can be determined, I just can not get it.

thank

+50
java enums instanceof
Nov 12 '10 at 16:00
source share
2 answers

The correct syntax is:

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

but for transfers, a more convenient method:

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



Update: for enumeration elements with a body (for example, for overriding methods) this will not work. In this case, use

 if (someObject instanceof Enum<?>) 

Reference:

+85
Nov 12 '10 at 16:03
source share

If you are talking about the new Java 5 function - enum (it is not very new in fact), then this is the way:

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

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

+14
Nov 12 '10 at 16:03
source share



All Articles