Skip to content

Java Closure

November 26, 2009

Here is an announcement from Sun – “ Closures in JDK7 ” which is followed by so many interesting discussions on why do we need it and why we don’t. I started looking for what is closure and this post is all about what I understood by the term closure.

A closure is a first class function with free variables bounded with the lexical environment. Ok, what is a First class function? From wiki, it means constructing new functions dynamically during execution of the program, storing them in data structures, passing them as arguments to other functions and returning them as results from other functions. Though Java doesn’t support this kind of programming, it can support Functors.

Functors are one way of achieving closures  and are nothing but functions encapsulated in objects. Functor instances play the role of pointer to functions in C++ (as call back functions are used).  In Java, function objects are expressed as interface whose implementation being an anonymous inner class and there are a lot of examples for this -comparator interface, Observer interface, runnable interface.

public interface MyComparator implements Comparable{

public int compare(int i1, int i2);

MyComparator  comparator1=new MyComparator(){
public int compare(int s1, int s2){
//compare s1 and s2 here
return 1;
//Use comparator1 to sort elements in the list
MyList.sort(new ArrayList(12,56),comparator1);

Here, MyCompartor is a class which implements the Comparable interface. The instance of MyComparator is a function object whose function is to say, compare two integers. The function object comparator1 is passed to the method sort to

Using the concept of Generics,

public interface MyComparator<I,O>{

public O compare(I i1, I i2);

MyComparator<String, Boolean> comparator2=new MyComparator<String, Boolean>(){
public Boolean compare(String s1, String s2){
//compare s1 and s2 here
return true;
//Use comparator1 to sort elements in the list
MyList.sort(new ArrayList(“hai”,”hi”),comparator2);

MyList.sort(new ArrayList(57,89,23,54),comparator1);

Functors also help in achieving code reusability by composition as in Decorator pattern,strategy pattern,command pattern and visitor pattern.

public enum Season{
SUMMER{ protected void display() {
System.out.println(“mar to may”);

WINTER{ protected void display() {
System.out.println(“nov to feb”);

SPRING{ protected void display() {
System.out.println(“jun to oct”);

protected abstract void display();

public void duration() {

Closures are often mistaken for the anonymous functions. Some programming languages, use anonymous functions to have the closures. But, the two are different. An anonymous function doesn’t capture the lexical environment where as the closure does. Hence, using closures can extend the lifetime of objects which may hinder the performance.

Here are some proposals for the syntax for Closures – .

One Comment leave one →
  1. January 28, 2010 5:18 am

    Thanks for posting such a terrific site. this blog was not only knowledgeable but also very inventive too. There normally are a limited number of bloggers who can create technical stuff that creatively. we look for articles about a subject like this. I have gone in detail through many websites to find knowledge regarding this.Keep writing in !!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: