This question has already been answered:
- Using anonymous class 1 answer
Here is a very simple code:
item.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); Everything seems to be easy. And I do not quite understand him. Even a little ashamed of it. In particular, I don’t understand this new ActionListener () statement. Here, after all, there is an operation new and parentheses .. As if we are creating an object. But you can't create an interface object. And then it is clear that an anonymous class is created here, not an object. Please explain why such a strange syntax and what is actually happening here?
UPD: It was written that this question may be a duplicate: Using an anonymous class But they understand what basically happens in the frame new ActionListener () {} And I understood that an object of an anonymous class was created there, I was wondering why the interface name is used for this.