How are Anonymous inner classes used in Java?

后端 未结 18 1696
孤独总比滥情好
孤独总比滥情好 2020-11-21 05:19

What is the use of anonymous classes in Java? Can we say that usage of anonymous class is one of the advantages of Java?

18条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 05:41

    Yes, anonymous inner classes is definitely one of the advantages of Java.

    With an anonymous inner class you have access to final and member variables of the surrounding class, and that comes in handy in listeners etc.

    But a major advantage is that the inner class code, which is (at least should be) tightly coupled to the surrounding class/method/block, has a specific context (the surrounding class, method, and block).

提交回复
热议问题