Java8 - How Lambda Expression Scope works with examples
In my previous posts, We already learned how to create a lambda expression with examples in java8.
This post is about the scope of variables and this/super keyword scope in lambda expressions with examples.
Lamda Expression Scope in java8
In Java, we have the following different scopes.
- Class Scope
Variables declared in a class can be accessed anywhere in a class.
- Method scope
variables declared in the method can be accessed anywhere in the declared method only.
- Enclosing scope or block
The variables are declared in the enclosing scope are only accessible in this block. examples are for each, while loops and static blocks.
Lambda Expressions are like anonyms block scope. so variables declared in lambda are only accessible in the lambda block.
Lambda Expression Local Variables Example in java8
Lambda Expression has a body, and it does not contain a new scope, but the body of expression scope is the same scope of enclosing code block.
For example, see the below code.
The local variable result is declared, if you declare the same local variable in the body of the lambda expression, the Compiler throws
Lambda expression’s local variable result cannot redeclare another local variable defined in an enclosing scope exception.
public class LambdaExpressionScope {
public static void main(String[] args) {
int result;
InterfaceOne inteOne=(a,b)->{
int result;
return a+b;
}
}
}
interface InterfaceOne{
int add(int a,int b);
}
a local variable must be initialized with a default value when declared in lambda expression or enclosing scope.
result
variable is declared, but not initialized. The compiler gives error The local variable result may not have been initialized
public class LambdaExpressionScope {
public static void main(String[] args) {
InterfaceOne inteOne=(a,b)->{
int result;
System.out.println(result);
return a+b;
};
}
}
interface InterfaceOne{
int add(int a,int b);
}
Lambda final effectively final local variable example
effectively final local variable are variables once declared and initialized, can not reassign with other values.
for example, Local Variables can be declared in a lambda expression and initialized with a value, but this is not declared with final and not updating its value, printing its value to console. It is called effectively final declaration
.
It is a correct and legal way to declare it in the lambda expression.
These variables can be declared in enclosing scope or lambda expression enclosing scope.
public class LambdaExpressionFinalExampleScope {
public static void main(String[] args) {
int result=30; // default final variable
InterfaceOne inteOne=(a,b)->{
// int result=30; you can also declare here, default final variable
System.out.println(result);
return a+b;
};
}
}
interface InterfaceOne{
int add(int a,int b);
}
Now modify the final variable in a lambda expression, reassign the local variable with 45.
This gives compilation error - Local variable result defined in an enclosing scope must be final or effectively final
int result=30;
InterfaceOne inteOne=(a,b)->{
result=45;
System.out.println(result);
return a+b;
};
Lamda expression with this and super keyword example
if this
keyword is used in the lambda expression. They act the same as we declared methods, not on the lambda body scope.
Lamda expression will not create a new scope, the this
keyword will use method scope where lambda expressions are defined.
Conclusion
In this tutorial, Learned lambda expression scopes local and effectively final, and this scope