The compiler automatically generates a constructor for your anonymous inner-class, and passes your local variable into this constructor.
The constructor saves this value in a class variable (a field), also named i
, which will be used inside the "closure".
Why it has to be final? Well let's explore the situation in where it isn't:
public class A {
public void method() {
int i = 0; // note: this is WRONG code
doStuff(new Action() {
public void doAction() {
Console.printf(i); // or whatever
}
});
i = 4; // A
// B
i = 5; // C
}
}
In situation A the field i
of Action
also needs to be changed, let's assume this is possible: it needs the reference to the Action
object.
Assume that in situation B this instance of Action
is Garbage-Collected.
Now in situation C: it needs an instance of Action
to update it's class variable, but the value is GCed. It needs to "know" it's GCed, but that is difficult.
So to keep the implementation of the VM simpler, the Java language designers have said that it should be final such that the VM doesn't need a way to check whether an object is gone, and guarantee that the variable is not modified, and that the VM or compiler doesn't have to keep reference of all usages of the variable inside anonymous inner-classes and their instances.