Accessing class members from a lambda expression. Capturing variables in lambda expressions. Examples
Contents
- 1. Accessing class members from a lambda expression. What class elements can be accessed from a lambda expression?
- 2. Accessing fields and methods of a class from a lambda expression. Example
- 3. Declaring variables (instances) in the body of a lambda expression. Example
- 4. Capturing variables in lambda expressions. Finalized variables. Features
- 5. An example demonstrating the use of a terminated variable
- Related topics
Search other websites:
1. Accessing class members from a lambda expression. What class elements can be accessed from a lambda expression?
If a method is declared in a class that contains a lambda expression, then from this lambda expression you can access the following class elements:
- data fields (data members) of the class. This applies to both the data members of the class instance and the static data members;
- class methods. This applies to both instance methods and static methods;
- variables (instances) declared directly in the body of the lambda expression;
- variables (instances), declared in enclosing curly braces { }, in which the program code of the lambda expression is located. In this case, the variable (instance) is considered captured.
⇑
2. Accessing fields and methods of a class from a lambda expression. Example
In the example being accessed fields a, b of the lambda disposed in LambaFunction class.
// Functional interface interface IFunction { void Function(); } // Class containing a method with a lambda expression class LambdaFunction { int a; // class data field static int b; // static field // class method void PrintA() { System.out.println("a = " + a); } // static class method static void PrintB() { System.out.println("b = " + b); } // Method with lambda expression void MethodLambda() { // 1. Lambda-expression IFunction ref = () -> { // There is access to all these elements a = 8; LambdaFunction.b = 15; PrintA(); LambdaFunction.PrintB(); }; // 2. Invoke the Function() method of IFunction interface ref.Function(); } } public class Lambda { public static void main(String[] args) { // Client code - calling MethodLambda() LambdaFunction lf = new LambdaFunction(); lf.MethodLambda(); } }
The result of the program
a = 8 b = 15
The above example declares the LambdaFunction class. This class implements the MethodLambda() method, which contains the code for the lambda expression. The following elements are available from this lambda expression:
- internal field a;
- static field b;
- the instance method PrintA();
- static method PrintB().
⇑
3. Declaring variables (instances) in the body of a lambda expression. Example
In the body of a lambda expression, you can declare any variables or class instances. It is allowed to declare variables with the final modifier. It is forbidden to declare static variables (instances).
Example. The snippet below declares and uses an integer z variable in the body of the lambda expression.
... // lambda expression ref = () -> { int z; // declaring a variable in the body of a lambda expression // ... // the use of variable z z = 277; // ... };
⇑
4. Capturing variables in lambda expressions. Finalized variables. Features
When using lambda expressions, you can access variables from the enclosing scope, which is defined by curly braces { }. In this case, the variable being accessed is considered captured. Capturing a variable is the use of a variable in a lambda expression that is declared in the enclosing scope. For a variable to be captured from a lambda expression, the variable must be terminated. The term “completed” means that the variable must be declared with the access modifier final, for example, like this
final int value; // the completed variable is declared
The completed variable can be used in the body of the lambda expression. However, it is forbidden to change this variable. When trying to change the value of a final variable, the compiler will throw an error.
⇑
5. An example demonstrating the use of a terminated variable
The following code demonstrates the use of the completed variable x in the body of a lambda expression.
// Functional interface interface IFunction { void Function(); } // Class containing a method with a lambda expression class LambdaFunction { // Method with lambda expression void MethodLambda() { // 1. Before lambda expression final int x = 33; // variable in the method to be captured in the lambda expression int y = 55; // a variable that will not be captured in the lambda expression // 2. Lambda-expression IFunction ref = () -> { // variable x cannot be changed because it is declared as final // x = 77; - compile error // however, it can be used int z; z = x + 5; // now variable x is captured // Print z System.out.println("z = " + z); // z = 38 }; // Invoke the Function() method of IFunction interface ref.Function(); // 3. After lambda-expression // variable x is already captured (final), so it cannot be changed // x = 44; // variable x can only be read (used) System.out.println("x = " + x); // variable y is not captured y = 88; // therefore it can be changed System.out.println("y = " + y); } } public class Lambda { public static void main(String[] args) { // Accessing fields and methods of a class of lambda expressions LambdaFunction lf = new LambdaFunction(); lf.MethodLambda(); } }
⇑
Related topics
- Lambda expressions. Basic concepts. Functional interface. Examples
- Lambda expressions for generic functional interfaces. Examples
- Passing a lambda-expression to a method as a parameter. Examples
- Throwing exceptions in lambda expressions. Examples
⇑