Like me you may be curious about the "closures" or "lambda expressions" that were supposed to be in Java 7 and are now the centre-piece of Java 8 (due next year). Also like me you may have Googled for some info, and discovered a smattering of technical articles explaining the origins of the syntax, and giving some examples that didn't relate to any problem I recognised.

But yesterday I found some new info, and suddenly it all came together in one blinding flash of understanding - the key to it all is "functional interfaces".

Just in case anyone else is interested, here's my personal take on what it's all about:
Consider the typical anonymous inner class that takes up so much of the boring code in Swing, eg

      button.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            System.out.println("Click on " + e.getSource());
         }

      });

What's interesting is just how much of that code is unecessary stuff that the comnpiler already knows:

  • addActionListener only takes an ActionListener as a parameter, so it already knew any new parameter must be an ActionListener
  • ActionListener is an interface with only one method (a "functional interface"), so what follows must be an implementation of an actionPerformed method
  • actionPerformed's parameter must be an ActionEvent

In the end the only new info is the name we chose to give the parameter, and the actual body of the method.

Closures allow you to replace the code above with just the bits that the compiler doesn't already know; the parameter name and method body:

button.addActionListener( (e) -> System.out.println("Click on " + e.getSource()));

repeat that for every button and menu item in an app and see the code shrink!

You can also use existing methods...
Rather than put all your code inside an annonymous inner class, it's common to just call a method, eg

  button.addActionListener(new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent e) {
        doStuff(e);;
     }

  });

  public void doStuff(ActionEvent e) { ...

all that code, just to associate a method with a button!

With closures you can just supply the method name - everything else is already known to the compiler, even the parameter(s)...

button.addActionListener(this::doStuff);

This magic works with all one-method interfaces, eg creating a Runnable to start a new Thread, or creating a Comparator for a sort.

Obviously there's a lot more to it that that, but that's enough to convince me that I want to be using them NOW.
You can download an early access JDK and try this today from http://jdk8.java.net/lambda/

One more example I have to share with you all... it's the Swing "invoke later" that clutters every GUI's main method. Here is the closures version that defines a Runnable to create a new instance of Demo on the Swing thread...

SwingUtilities.invokeLater(Demo::new);

I want it NOW!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.