Java – Lambda Expression Variable Capturing with Examples
Last Updated :
28 Jul, 2021
Variable 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 (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 intrinsic or static variable and call a method define by its enclosing class.
Lambda expression in java Using a local variable is as stated.
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.
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.
There are certain keypoints to be remembered, which are as follows:
- Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared final or be effectively final , or a compile-time error occurs where the use is attempted.
- Any local variable used but not declared in a lambda body must be definitely assigned before the lambda body, or a compile-time error occurs.
- Similar rules on variable use apply in the body of an inner class . The restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems. Compared to the final restriction, it reduces the clerical burden on programmers.
- The restriction to effectively final variables includes standard loop variables, but not enhanced-for loop variables, which are treated as distinct for each iteration of the loop.
The following program illustrates the difference between effectively final and mutable local variables:
Example 1
Java
import java.io.*;
interface MyFunction {
int func( int n);
}
class GFG {
public static void main(String[] args)
{
int number = 10 ;
MyFunction myLambda = (n) ->
{
int value = number + n;
return value;
};
System.out.println( "GFG!" );
}
}
|
Output explanation:
As the comments indicate, number is effectively final and can, therefore, be used inside myLambda. However, if number were to be modified, either inside the lambda or outside of it, number would lose its effective final status. This would cause an error, and the program would not compile.
Example 2
Java
import java.io.*;
interface MyInterface {
void myFunction();
}
class GFG {
int data = 170 ;
public static void main(String[] args)
{
GFG gfg = new GFG();
MyInterface intFace = () ->
{
System.out.println( "Data : " + gfg.data);
gfg.data += 500 ;
System.out.println( "Data : " + gfg.data);
};
intFace.myFunction();
gfg.data += 200 ;
System.out.println( "Data : " + gfg.data);
}
}
|
Output
Data : 170
Data : 670
Data : 870
Note: 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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...