What is inclusive area in Java
The protocol of the local variables defined in an enclosing area must be final or effectively final
The reason for this limitation is same reason for the Java language feature that local variables must be accessed when (effectively) final from (anonymous) inner classes is retrieved.
This answer from rgettman goes into details. rgettman explains the restrictions in detail, and I'll tie in with this answer because the behavior of lambda expressions should be the same as that of anonymous inner classes. Note that this restriction does not apply to class or instance variables. The main reason for this is a bit complicated and I can't explain it better than what Roedy Green is doing here. Copy here just so that it is in one place:
The rule is anonymous. Inner classes may only access the local ending .__. Inclusive method variables. Why? Because the inner class methods can be called later, long after the method that created them has finished, e.g. by an AWT event (Advanced Windowing Toolkit). The local variables are long gone. The anonymous class must then work with .__. flash frozen copies of those obscured by the compiler in the anonymous inner class object. You might ask, why do the local variables have to be final? Could the compiler not also take a copy of the non-final local variable, much like it does for a non-final parameter? In this case you would have two copies of the variable. Each can change independently, much like the caller and emergency copy of a parameter, but you would use the same syntax to access both copies. That would be confusing. So Sonne insisted that the locals be final. This makes it irrelevant that there are .__. actually two copies of it.
The possibility for an anonymous class to access the local terminal of the caller is actually just syntactic sugar for the automatic transfer of some local variables as additional constructor parameters. The whole thing smells like diluted eau de kludge to me.
- What would happen if Hollywood was bombed?
- The feeling of emotions is unique to humans
- How were Judah and Jesus Christ related?
- What is the weakness of Eminem
- What are pigs used for?
- Will do a PhD under MBBS
- How do I stay healthy during unemployment
- Mosturizer for dry to normal skin
- What's the Best FMCG Business
- How do I sell hotels
- What is underestimated in India
- Travel often
- You can improve your memory by practicing
- What is competition dance
- Obi Wan Kenobi had children
- Which words sound better when pronounced incorrectly?
- Changes Quora 's responses to censorship information
- Is Trump a fear maker
- How do tattoos negatively affect athletic performance?
- What is perceived value for the customer
- How should an INTP stop being lazy
- How do I find log 10 526
- Which wine goes well with the fondue?
- How can I get NCD