let's play TDD, tackles closures in java

Brian Goetz brian.goetz at oracle.com
Mon Dec 20 02:42:33 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);
>         }
>     }

Yuck.  Not only does it use the hard-to-read instance initializer feature, but it allows the 'this' reference to escape during construction, which is a no-no.




More information about the lambda-dev mailing list