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

后端 未结 6 1083
遥遥无期
遥遥无期 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 10:50

    you could always:

    getClass().getName()
    

    and do a String comparison.

    EDIT : to account for inheritance (among inner classes? who would do that?!) you could always loop through getSuperclass() and check for them as well, and even go after implemented interfaces.

提交回复
热议问题