How are Anonymous inner classes used in Java?

后端 未结 18 1323
孤独总比滥情好
孤独总比滥情好 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:42

    GuideLines for Anonymous Class.

    1. Anonymous class is declared and initialized simultaneously.

    2. Anonymous class must extend or implement to one and only one class or interface resp.

    3. As anonymouse class has no name, it can be used only once.

    eg:

    button.addActionListener(new ActionListener(){
    
                public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    });
    

提交回复
热议问题