I'm trying to change the boolean variable inside java8 forEach loop to true which is non final. But I'm getting following error : Local variable required defined in an enclosing scope must be final or effectively final.
How to resolve this error?
Code :
boolean required = false;
This is the variable I have created in the function.
Now when I'm trying to change it :
map.forEach((key, value) -> {
System.out.println("Key : " + key + " Value : " + value);
required = true;
});
I'm getting the error : Local variable required defined in an enclosing scope must be final or effectively final.
Why this error is arising and how to resolve it?