Bringing Closures to Java 5, 6 and 7

Need closures on your Java platform? There is no need to wait for Java 8!         

This code is no longer availabe on Github. It is now availabe ( currently not with the full set of features in the original one ( interphase version instead focuses on Java 8 support ) , but it is hosted on Bitbucket )

Coming soon more features build on top of this such as libaries.

Hosted on 
GIT hub

Basically, this library gives you the ability to strictly define a one method interface on the fly.

It also supports passing of non-final variables, voids, unlimited number of parameters, and prematurely breaking from a loop.

It is a one file implementation, for usage simplicity and works as a class container with public and hidden private classes. I have supplied three implementations (actually made a couple more but were dismissed)
Most simple to use and understand version is v0but my favourite of the three is v1 though, but have a look at v2 as well which is a bit more dynamic, whereas v0 and v1 are fully static.


To run the examplescheck out the entire project, open a command prompt, standing at the root directory (where org, and examples directories are) and run:

>> javac examples/v1/
>> java  examples.v1.ClosureExamples


If you are interested in helping out with shaping this library, the utility library, improve the implementation, or provide more versions just sent me an email at mohamed.seifeddine _at_ gmail dot com and I will grant you access to the repo.

Functional Java

Ok, some of you might suggest that this already exists in Functional Java. This isn’t true. Focus there seems to be on doing things like sort, filter, exits, map, foldRight, left and what not.

When you want more than those predefined, it is hard to figure out how to do something like this implementation offers.

Same thing applies to the libraries of Guava and Lambdaj. Their focus are not on bringing closures to the Java world, but more on functionality similar to Functional Java.


Intellij IDEA is preparing for the project lambda, so it will be easy to convert the above to real lambdas:
Intellij will also, already from today show the Closure in the editor in a more compact mode.

In Intellij 12 this piece of code:

Is by default minimized as a compact Java 8 lambda:

Project lambda

Current syntax suggestion for the project lambda is pretty ugly, and hard to understand. There are too many ways a lambda/closure can be defined, it should be more coherent and consistent which will make it hard for novice developers to adopt its usage I am afraid.

I kind like how it looks like on this old presentation I found.


  1. Cool! Exactly what I have been looking for!

  2. A bit verbose but incredibly powerful! Nice effort!

  3. Very nice! Yes, as someone else said, it's verbose, but Java is verbose sometimes. I'm really looking forward to lambdas in Java 8! Look me up and introduce yourself sometime as I don't immediately see how to find out who you are.

    1. It is as thin as it can be using Java.
      With Java 7, there is also generic type interference on the right hand side on instance creation so there is no need for repeating the generic part.

      Also, using import static*;

      we could also do

      new R1<>(){} rather than new Closure.R1<>(){ } everywhere.


    1. Yes, I have seen the functionaljava library before, and initially this blog post contained a reference to it.

      Have you seen the size of that library, any idea of how to actually use it? It tries to do so much more than just provide Closures to the Java world, yet fails to provide the most basic.

    2. functionaljava.jar 1,339kb

    3. Size meaning number of classes and too many methods. Functional java tries to be functional, but really I just want Closure support. Java compiled class files is not the way to measure that :)

      Functional java focus seems to be on doing things like sort, filter, exits, map, foldRight, left and what not.
      When you want more than those predefined, it is hard to figure out how to do something like this implementation does.

      This one supports passing of non-final variables, voids, unlimited number of parameters if you want.
      Functional java is intended for something else, I am not sure it even provides "Closures" or something similar as this to the java world.

      Could you show me how I could do an eachLine on a file, eachFileRecure, findFileRecurse that takes a "Closure"? I am unable to figure that part out.

      I cant even figure out which class is actually supposed to provide something similar as the implementation here.

      You know, the car might already be invented, if that is even the case here(?), but they still build new and better ones.

  5. Why 20 as the number of maximum static parameters before you move on using varargs?

  6. Yes you are right! This should probably be higher, but I didn't want to push it to far either. What do you think is a better number?