let's play TDD, tackles closures in java

Llewellyn Falco isidore at setgame.com
Wed Dec 15 10:55:37 PST 2010

> The long, special-purpose way around, using anonymous inner classes (or
> local classes), is actually rather easy.
>     new XyzListener() {
>         private boolean fired;
>         public void event(XyzEvent event) {
>             fired = true;
>         }
>         {
>             abc.addXyzListener(this);
>             abc.doThing();
>             assertTrue(fired);
>         }
>     }
> Tom
> Yes, those 8 lines of code do seem easy compared to

boolean fired = false;
model.addTableModelListener(#{e -> fired = true});

not that this isn't a neat trick to run the block inside an
aynomous constructor,  but it both longer, and much more importantly, harder
to read. The flow of what is currently being executed is confused.

the point is, once again we have are going to have an industry standard
recommendation to write the following code:

boolean fired[] = {false};
model.addTableModelListener(#{e -> fired[0] = true});

The "language style" side of me cringes at this.
Llewellyn Falco

More information about the lambda-dev mailing list