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
?
In order to make an inner class behave as if it is actually defined inside another class, the Java compiler ends up inserting hidden fields, methods, and constructor arguments into the classes it generates.
A closure is a function (or method) that refers to free variables in their lexical context. The function is a block of code with parameters. It may produce a result value (return type). A free variable is an identifier used but not defined by the closure.
Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.
You can just think protected inner class is protected member, so it only access for class, package, subclass but not for the world. In addition, for outter class, there is only two access modifier for it. Just public and package.
Local variables are (obviously) not shared between different methods such as method()
and doAction()
above. But since it's final, nothing "bad" could happen in this case, so the language still allows it. The compiler however, needs to do something clever about the situation. Lets have a look at what javac
produces:
$ javap -v "A\$1" # A$1 is the anonymous Action-class. ... final int val$i; // A field to store the i-value in. final A this$0; // A reference to the "enclosing" A-object. A$1(A, int); // created constructor of the anonymous class Code: Stack=2, Locals=3, Args_size=3 0: aload_0 1: aload_1 2: putfield #1; //Field this$0:LA; 5: aload_0 6: iload_2 7: putfield #2; //Field val$i:I 10: aload_0 11: invokespecial #3; //Method java/lang/Object."<init>":()V 14: return ... public void doAction(); Code: Stack=2, Locals=1, Args_size=1 0: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream; 3: aload_0 4: getfield #2; //Field val$i:I 7: invokevirtual #5; //Method java/io/PrintStream.println:(I)V 10: return
This actually shows that it
i
variable into a field, A
objectdoAction()
method.(A side note: I had to initialize the variable to new java.util.Random().nextInt()
to prevent it from optimizing away a lot of code.)
Similar discussion here
method local innerclasses accessing the local variables of the method
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With