Why do we use final keyword with anonymous inner classes?

巧了我就是萌 提交于 2019-11-28 08:59:36
brain

Your intuition is correct, because the variable is final it is safe to make a copy of it. Of course for reference types this means copying the reference to the object and not the object it refers to.

The compiler uses subtle trickery copying the final reference under the covers to let the inner class get to the final field in the outer class. The copying is why the field must be final so the value does not change.

See e.g. http://tech-read.com/2008/06/19/why-inner-class-can-access-only-final-variable/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!