Lambda expressions - Effectively final local variables

Effectively final local variables 

To capture variables from enclosing context in lambda expression body, the variables must be final or effectively final. final variables are declared with final modifiers. effectively final variables are never assigned after their initialization. Code in below has compilation error, because variable name is assigned again after its initialization, so name cannot be referenced in lambda expression body.

Compilation error of using non-final variables in lambda expression body 

public void run() { 
String name = "Alex"; 
new Thread(() -> System.out.println("Hello, " + name)).start(); 
name = "Bob"; 
}


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations