Does the Java ClassLoader load inner classes?

后端 未结 5 1602
迷失自我
迷失自我 2020-12-08 10:50

If I have a inner class declaration such as:

Class A {
    public static class B {
    }
}

followed by:

Class impl         


        
5条回答
  •  猫巷女王i
    2020-12-08 10:50

    A ClassLoader will not load a class, unless it was requested (e.g. using loadClass). While loading a class, a ClassLoader will request referenced classes.

    As your class A does not reference A.B, A.B will not be loaded, whether it is static or not. (To be honest, A does reference A.B, but not in a manner causing the ClassLoader to load A.B.)

    If you add a field of type A.B or use the type A.B in another way (e.g. as a method return type), it will actually be referenced in A.class and therefore be loaded.

提交回复
热议问题