A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-lambda-expression-variable-capturing-with-examples/ below:

Java - Lambda Expression Variable Capturing with Examples

Java - Lambda Expression Variable Capturing with Examples

Last Updated : 23 Jul, 2025

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: 

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

Example 1: Effectively Final vs. Mutable Local Variables Java
// 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!");
    }
}
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
// 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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4