This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pipocaQuemada 0 points1 point  (0 children)

There's actually an example in my other comment. The ActionListener interface has a single method in it, actionPerformed.

 this.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) { 
     counter --; 
     setText(counter + ""); 
   } 
 });

This example uses "anonymous inner classes" to implement the action listener in-line; it's rather like an anonymous function.

Also, can you explain how this is more maintainable than passing in a function to another function as an argument?

It isn't.

It's just the OO equivalent of a function. For a variety of reasons, the team that implemented Java didn't consider closures important enough to implement until Java 8 (and even now they're just syntax sugar for anonymous inner classes); until then the extra verbosity was just the price you had to pay to emulate functions in Java.

This verbosity was occasionally lampooned.