Java: How to check if an object is an instance of a non-static inner class, regardless of the outer object?

后端 未结 6 1067
遥遥无期
遥遥无期 2021-02-05 10:35

If I have an inner class e.g.

class Outer{
    class Inner{}
}

Is there any way to check if an arbitrary Object is an instance of

6条回答
  •  一整个雨季
    2021-02-05 11:07

    And what about?

    public static boolean isInnerClass(Class clazz) {
        return clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers());
    }
    

    The method isMemberClass() will test if the method is a member (and not an anonymous or local class) and the second condition will verify that your member class is not static.

    By the way, the documentation explains the differences between local, anonymous and nested classes.

    Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.

提交回复
热议问题