Checking if a class is java.lang.Enum

后端 未结 2 1405
逝去的感伤
逝去的感伤 2021-02-01 11:49

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         


        
2条回答
  •  梦谈多话
    2021-02-01 12:43

    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()

提交回复
热议问题