inner class uses outer local variable is through its constructor like this InnerClass(String outerVar)
inner class uses outer field is through this
.
I read it from an article that programmers who use Java are not used to to having variable changed both sides, otherwise it causes a AAD (Action At remote Distance).
If that is true, outer field can be changed both sides causes the same AAD problem.
What are the thinkings behind local var is final while field is not.
void test() {
var a = "a"; // outer local variable
class InnerClass {
void print() {sout(a);} // InnerClass(String a)
}
}
String a = "a" // outer field
void test() {
class InnerClass {
void print() { a = newA;} // OuterClass.this.a = newA
}
sout(a) // newA
}