3 ms·
Java anon classes couldn't capture non-final locals back when they were introduced. So they were closures, but not full-fledged. In contrast, C# anon delegates
by int_19h 9y ago
Java anon classes couldn't capture non-final locals back when they were introduced. So they were closures, but not full-fledged. In contrast, C# anon delegates could capture any local, including writeable ones.
- dtech 9y agoThey still can't, captured variables need to be "effectively final" since Java 8, meaning they aren't allowed to be written after the capture. I think that's reasonable though, capturing mutable state is a recipe for disaster, and if you really want/need to you can wrap it in a properly synchronized container (e.g. AtomicReference in Java)
- int_19h 9y agoIn most cases, lambdas that capture mutable state effectively take ownership of it, so there's no issue there.