Home | | Web Programming | Lambda Expressions and Variable Capture

Chapter: Java The Complete Reference : The Java Language : Lambda Expressions

Lambda Expressions and Variable Capture

Variables defined by the enclosing scope of a lambda expression are accessible within the lambda expression.

Lambda Expressions and Variable Capture

 

Variables defined by the enclosing scope of a lambda expression are accessible within the lambda expression. For example, a lambda expression can use an instance or static variable defined by its enclosing class. A lambda expression also has access to this (both explicitly and implicitly), which refers to the invoking instance of the lambda expression’s enclosing class. Thus, a lambda expression can obtain or set the value of an instance or static variable and call a method defined by its enclosing class.

However, when a lambda expression uses a local variable from its enclosing scope, a special situation is created that is referred to as a variable capture. In this case, a lambda expression may only use local variables that are effectively final. An effectively final variable is one whose value does not change after it is first assigned. There is no need to explicitly declare such a variable as final, although doing so would not be an error. (The this parameter of an enclosing scope is automatically effectively final, and lambda expressions do not have a this of their own.)

 

It is important to understand that a local variable of the enclosing scope cannot be modified by the lambda expression. Doing so would remove its effectively final status, thus rendering it illegal for capture.

The following program illustrates the difference between effectively final and mutable local variables:

 

// An example of capturing a local variable from the enclosing scope.

 

interface MyFunc { int func(int n);

 

}

 

class VarCapture {

 

public static void main(String args[])

 

{

 

// A local variable that can be captured.

int num = 10;

 

MyFunc myLambda = (n) ->        {

 

     This use of num is OK. It does not modify num. int v = num + n;

 

     However, the following is illegal because it attempts

 

     to modify the value of num.

 

     num++;

 

return v;

 

};

 

     //The following line would also cause an error, because

 

     //it would remove the effectively final status from num.

 

     num = 9;

 

}

 

}

 

As the comments indicate, num is effectively final and can, therefore, be used inside myLambda. However, if num were to be modified, either inside the lambda or outside of it, num would lose its effectively final status. This would cause an error, and the program would not compile.

 

It is important to emphasize that a lambda expression can use and modify an instance variable from its invoking class. It just can’t use a local variable of its enclosing scope unless that variable is effectively final.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Language : Lambda Expressions : Lambda Expressions and Variable Capture |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.