Variables in lambda`s must be effectively final. Ok. But why it is allowed to assign class field value in example code below?
Does heap and stack cause this difference? Why?
class Scratch
{
public String field = "class field";
public void example()
{
Function<String, Integer> funcWithField = s ->
{
field = "New field value from lambda";
// IT`s OK here
return field.length();
};
String variable = "var";
Function<String, Integer> funcWithVariable = s ->
{
// Though such things are restricted!
// variable = "some other loooong value for variable";
return variable.length();
};
}
}