3 ms·
The inner class gets copies of the variables, so imperative code that wants to reassign them isn't allowed because it probably won't do what you expected. The
by erik_seaberg 7y ago
The inner class gets copies of the variables, so imperative code that wants to reassign them isn't allowed because it probably won't do what you expected.
The goal is not to GC stack frames. But I'm not sure why the didn't create an inner class to hold the closed-over variables in non-final fields (moving them from the stack to the heap) for both the function and all closures it creates.
(Obligatory "doctor, it hurts when I use mutable state!")
- kjeetgill 7y agoAh, gotcha. Honestly, I always use this as an example of one of the subtle design points that I really appreciate Java for. Nitpick, but saying copies in Java can get confusing. Both primitives and references are bound by value. I'm sure you know, but for others: no objects are copied. I always found this limitation had reassuring regularity; it's the same way arguments are bound to function parameters (minus bring final). Local variables being isolated from "other scopes" means that any interthread communication must be mediated through objects.