-2

It seems as if an ActionListener object were created with an inherited/overridden method. Has this syntax some special name?

btnLocationDev.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
               ...
    }
});
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
peterh
  • 11,875
  • 18
  • 85
  • 108

3 Answers3

3

It's called anonymous inner class. You just created an instance of anonymous inner class which implements ActionListener.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
3

It is an anonymous inner class. .

peterh
  • 11,875
  • 18
  • 85
  • 108
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
3

That is anonymous Inner class. It can actually come in handy when making an instance of an object which something extra such as overloading methods, without having to actually subclass a class. I use it mostly when i want to attach a listner.

When you conventionally attach a listner you have to overload each method but when you use annonymous inner class you just overload the method you want.

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
        // Method to be used.
    }
});
Rohan
  • 673
  • 6
  • 15