Wednesday, August 22, 2018

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 Basics

In Java, we have different scopes.
Lambda Expression local variable, this,super scope
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
Enclosing scope or block 
The variables are declared in enclosing scope are only accessible in this block. for each, while, static blocks

Lamda Expressions are like anonyms block scope. so variables declared in lambda only accessible in the lambda block.

Lamda Expression Local Variables Example

Lamda Expression has a body, will not have a new scope, but body of expression scope is same scope of enclosing code block.
For example, see the below code. The local variable result is declared, if you declare a same local variable in boday of the lambda expression, Compiler throws
Lambda expression's local variable result cannot redeclare another local variable defined in an enclosing scope exception.
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 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);
 
}

final effectively final local variable example

effectively final local variable are variables once declared and initialized, can not reassign with other value.
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. This are called effectively final declaration. This 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 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;
 };
}
}
Now modify the final variable in lambda expression, reassign 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, this keyword will use method scope where lambda expressions are defined.

Related article


EmoticonEmoticon