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

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);

No comments:

Post a Comment