I’ve read several online articles which contradict each other. I thought this would be an example of an anonymous inner class:
button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e) {
// do something.
}
});
However, I’ve also seen this described as an anonymous inner class:
ActionListener myListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
// do something.
}
};
button.addActionListener(myListener);
Which is which, and why? Thank you!
Both of them are. The second one is just assigned to a variable before being added as an action listener.
This is the same as the difference between
and
it has nothing to do with anonymous classes.