March 29, 2013

Java and Closures

I found that java is in the process of adding function closures (function as first class citizen) to its syntax.

Here is a good example

http://java.amitph.com/2012/08/at-first-sight-with-closures-in-java.html

public class a {
    public String variable = "Class Level Variable";
    public static void main(String[] arg) {
        new a().lambdaExpression();
    }
    public void lambdaExpression(){
        String variable = "Method Local Variable";
        String nonFinalVariable = "This is non final variable";
        new Thread (() -> {
            //Below line gives compilation error
            //String variable = "Run Method Variable"
            System.out.println("->" + variable);
            System.out.println("->" + this.variable);
       }).start();
    }
}

No comments:

Post a Comment