In Java an anonymous inner class can refer to variables in it’s local scope:
public class A {
public void method() {
final int i = 0;
doStuff(new Action() {
public void doAction() {
Console.printf(i); // or whatever
}
});
}
}
My question is how is this actually implemented? How does i get to the anonymous inner doAction implementation, and why does it have to be final?
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:
In situation A the field
iofActionalso needs to be changed, let’s assume this is possible: it needs the reference to theActionobject.Assume that in situation B this instance of
Actionis Garbage-Collected.Now in situation C: it needs an instance of
Actionto 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.