Checking if a class is java.lang.Enum

时光怂恿深爱的人放手 提交于 2019-12-02 21:33:52

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:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!