What is an Anonymous inner class? Explain with an example
Anonymous Inner ClassesAn inner class without a name. It allows the declaration of the class, creation of the object and execution of the methods in it at one shot. The typical use of inner class is widely used in GUI event handling.
For examplebutton.addActionListener(new ActionListener()
{
// The way of using Anonymous Inner class
public void actionPerformed(ActionEvent e)
{
System.out.println("The button was pressed!");
}
}
In the above code snippet, there a definition of a new class while invoking another method. By utilizing an anonymous inner class, the hard coding of defining a separate class and its methods and invoking them by creating an exclusive object explicitly, is reduced to almost a single line within its containing class.