Strange syntax for instantiating an inner class

前端 未结 6 1838
慢半拍i
慢半拍i 2020-12-05 09:12

I didn\'t imagine that I would encounter radically new syntax in Java anymore at this stage, but lo and behold, I just encountered something:

The exact context a

6条回答
  •  天涯浪人
    2020-12-05 10:06

    A non-static inner class in Java contains a hidden reference that points to an instance of the outer class it is declared in. So the error message you got originally is telling you that you cannot create a new instance of the inner class without also specifying an instance of the outer class for it to be attached to.

    Perhaps the reason you haven't seen that syntax before is that inner classes are often allocated in a method of the outer class, where the compiler takes care of this automatically.

提交回复
热议问题