Open In App

Java – Lambda Expression Variable Capturing with Examples

Last Updated : 24 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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 defined 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 effective 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 effective final status, thus rendering it illegal for capture.

There are certain key points 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: Effectively Final vs. Mutable Local Variables

// Java Program Illustrating Difference between
// Effectively final and Mutable Local Variables

// Importing reqiored classes
import java.io.*;
// An example of capturing a local variable from the
// enclosing scope

// Inrterface
interface MyFunction {

    // Method inside the interface
    int func(int n);
}

// Main class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Custom local variable that can be captured
        int number = 10;

        MyFunction myLambda = (n) ->
        {

            // This use of number is OK It does not modify
            // num
            int value = number + n;

            // However, the following is illegal because it
            // attempts to modify the value of number

            // number++;
            return value;
        };

        //Using the Lambda expression 
		 System.out.println(myLambda.func(20));
        System.out.println("GFG!");
    }
}

Output
30
GFG!

Explanation of the Program: 

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: Using Instance Variables in Lambda Expressions

// Java Program Illustrating Lambda Expression with Instance Variables 

import java.io.*; 

// Interface 
interface MyInterface { 
    void myFunction(); 
} 

// Main class 
class GFG { 
    // Custom initialization
    int data = 170; 

    // Main driver method 
    public static void main(String[] args) { 
        // Creating object of this class 
        GFG gfg = new GFG(); 

        // Creating object of interface 
        MyInterface intFace = () -> { 
            System.out.println("Data: " + gfg.data); 
            gfg.data += 500; 
            System.out.println("Data after modification: " + gfg.data); 
        }; 

        // Using the lambda expression
        intFace.myFunction(); 

        // Modifying the instance variable
        gfg.data += 200; 
        System.out.println("Final Data: " + gfg.data); 
    } 
}

Output
Data: 170
Data after modification: 670
Final Data: 870

Explanation of the Program:

In this example, the lambda expression modifies an instance variable data of the enclosing class GFG. Unlike local variables, instance variables are not subject to the effectively final restriction, so they can be modified both inside and outside the lambda expression.

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.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg