Using an inner class name and an object name same in Java

前端 未结 3 908
执念已碎
执念已碎 2021-02-13 23:31

In the following code snippet, presumably it appears that it should issue some compilation error but it doesn\'t:

class Outer {
    public static class Inner {
          


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-13 23:45

    Actually the class name is Outer$Inner.

    Inner classes are essentially a hack introduced in Java 1.1. The JVM doesn't actually have any concept of an inner class, and so the compiler has to bodge it. The compiler generates class B "outside" of class A, but in the same package, and then adds synthetic accessors/constructors to it to allow A to get access to it.

    Checkout the following post:

    Java inner class visibility puzzle

提交回复
热议问题