0

I am really confused while using Lambda expression in my code. I have declared a variable outside of the lambda expression and while using it inside the lambda expression, I am getting exception as:

Local variable var1 defined in an enclosing scope must be final or effectively final

Below is my code:

String var1=null;
String var2;
        
List<String> list = new ArrayList<String>();
list.add("Apple");
list.add("Banana");
list.add("Peach");
        
list.forEach( (obj) -> { // Outer lambda expression
    String var3; //This is declared inside of outerloop but outside of inner loop
    list.forEach( (obj2) -> { // Inner lambda expression
        var1="With null initialization"; // giving error
        var2="Without initialization"; // giving error
        var3="var3"; // giving error
    });
});

So firstly, how can I remove this error and also declare the variable outside of the lambda expressions?

Secondly: About var3, when I remove the declaration of var1 and var2, the error from var3 disappears which is really strange to me. Why is it giving error when var1 and var2 is declared outside of the scope. How possibly var3 is linked with var1 and var2.

//String var1=null;
//String var2;
List<String> list = new ArrayList<String>();
list.add("Apple");
list.add("Banana");
list.add("Peach");

list.forEach( (obj) -> { // Outer lambda expression
String var3; //This is declared inside of outerloop but outside of inner loop
    list.forEach( (obj2) -> { // Inner lambda expression
        var3="var3"; // error is gone when var1 and var2 declaration is removed
    });
});
Abra
  • 19,142
  • 7
  • 29
  • 41
Eatsam ul haq
  • 317
  • 1
  • 12
  • 1
    Does this answer your question? [Java 8 stream variable used in lambda should be final or effectively final](https://stackoverflow.com/questions/54238974/java-8-stream-variable-used-in-lambda-should-be-final-or-effectively-final) – Nishikant Tayade Mar 08 '22 at 10:14
  • 1
    When you say "I am getting exception" do you mean you are getting a compilation error? Exceptions and compiler errors are two completely different things. – k314159 Mar 08 '22 at 10:17
  • Yes I am getting Compilation exception. I am sorry to be confusing – Eatsam ul haq Mar 08 '22 at 10:19
  • For the first problem, I understand that there are several solutions there and some might be helpful as well. But for the second problem. Please tell me a reason of its occurance – Eatsam ul haq Mar 08 '22 at 10:23
  • Cannot reproduce the other issue, `var3` is still a compiler error. – luk2302 Mar 08 '22 at 10:27
  • To simply put, with only declaration of `var1` and `var2`, I am getting compilation error on `var3`. How does these two lines affecting `var3`? – Eatsam ul haq Mar 08 '22 at 10:30
  • 1
    var1 and var2 don't affect var3. The error on var3 is still there when you remove var1 and var2, as you can see at https://ideone.com/kdI1Pr – k314159 Mar 08 '22 at 10:37
  • Ok I see that. When I was removing it the compiler was not compiling `var3` therefore the error was occurring on `var1` and `var2` as they were not declared. And I thought the error from `var3` is resolved. Sorry my bad. – Eatsam ul haq Mar 08 '22 at 10:42

0 Answers0