3 ms·
This is roughly how Java works with anonymous types closing over variables also. That's why the variables must be declared `final`. It just copies the local v
by jdmichal 8y ago
This is roughly how Java works with anonymous types closing over variables also. That's why the variables must be declared `final`. It just copies the local values over into the anonymous type and calls it a day. Of course, since the only thing allocated on the stack are primitives and pointers, and everything on the heap is subject to garbage collection, this is a pretty straight-forward operation.
I don't know if lambdas work the same way. I know in some ways they work like anonymous types, and not in others.
- _old_dude_ 8y agoyes, it works the same way with lambdas, the lambda proxy (the class that implements the functional interface) contains the copy of the local values. Here is the code that generate the constructor of a lambda proxy http://hg.openjdk.java.net/jdk/jdk/file/3cabb47758c9/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java#l356 http://hg.openjdk.java.net/jdk/jdk/file/3cabb47758c9/src/jav...