Java (anonymous or not) inner classes: is it good to use them?

后端 未结 15 2332
鱼传尺愫
鱼传尺愫 2020-12-01 06:48

In some of my projects and in some books was said to not use inner class (anonymous or not, static or not) - except in some restricted conditions, like

15条回答
  •  难免孤独
    2020-12-01 07:17

    One item that is not mentioned here is that a (non-static) inner class carries a reference to it's enclosing class. More importantly, the inner class has access to private members of it's enclosing class. It could, potentially, break encapsulation.

    Don't use an inner-class if you have an option.

提交回复
热议问题