Lambda scopes in java 8













1) Final variable can be accessed in lambda expression in java 8 (Behavior same as anonymous inner class)


@FunctionalInterface
interface CalculatorInterface {
   Integer sumMethod(Integer val1, Integer val2);
}
public class LambdaExpressionScopeExample {
   public static void main(String[] args) {
          // Final Local variable can be accessed in lambda expression in java 8
          final int x = 1; // Final Local variable
          // Provide implementation (definition) of sumMethod - using Lambda expression
          CalculatorInterface sum = (val1, val2) -> {
                 return val1 + val2 + x;
          };
          // Call sumMethod
          Integer result = sum.sumMethod(2, 3);
          System.out.println("sumResult = "+result); // 6
   }
}
/*
sumResult = 6
*/


2) non-Final variable can be accessed in lambda expression in java 8
Though non-Final variable can be accessed in lambda expression, But it is effectively final in lambda expression in java 8 ( Again Behavior same as anonymous inner class).


So, you use non-final variable in Lambda expression BUT you cannot modify x in lambda expression.


Any attempt to modify x will produce compilation error.


What will be exact compilation error in eclipse in java 8 ?
“Local variable x defined in an enclosing scope must be final or effectively final.”


@FunctionalInterface
interface CalculatorInterface {
   Integer sumMethod(Integer val1, Integer val2);
}
public class LambdaExpressionScopeExample {
   public static void main(String[] args) {
          // Though non-Final local variable can be accessed in lambda expression
          // But it is effectively final in lambda expression.
          //So, you cannot modify x in lambda expression.
          //Any attempt to modify x will produce compilation error in java 8.
          int x = 1; // Non-Final Local variable
          CalculatorInterface sum = (val1, val2) -> {
                x = x + 1; //COMPILATION ERROR
                return val1 + val2 + x;
          };

   }
}


Interesting to see very important internal working : why local class does not allow access to non-local final variable.

3) Accessing Instance and static variable in Lambda Expression in java 8 >
  1. Instance variable can be accessed (and modified unlike local variable) in lambda expression in java 8
  2. Static variable can be accessed (and modified unlike local variable) in lambda expression in java 8


@FunctionalInterface
interface CalculatorInterface {
   Integer sumMethod(Integer val1, Integer val2);
}
public class LambdaExpressionScopeExample {
   // Instance variable can be accessed in lambda expression in java 8
   int instanceVariable = 1; // Instance variable
  
   // Static variable can be accessed in lambda expression in java 8
   static int staticVariable = 2; // Static variable
  
   public static void main(String[] args) {
         
          LambdaExpressionScopeExample obj = new LambdaExpressionScopeExample();
          // Provide implementation (definition) of sumMethod - using Lambda expression
          CalculatorInterface sum = (val1, val2) -> {
                return val1 + val2 +
                   + obj.instanceVariable // Access Instance variable in lambda expression
                   + staticVariable;  // Access static variable in lambda expression
          };
          // Call sumMethod
          Integer sumResult = sum.sumMethod(2, 3);
          System.out.println("sumResult = "+sumResult); // 8
   }
}
/*OUTPUT
sumResult = 8
*/

Labels: Java 8
Must read for you :