Local class can access non-final variable in java 8

久未见 提交于 2019-11-28 11:57:21

The situation has not changed at all, actually. The compiler is just a bit smarter, and doesn't force you to use the final keyword anymore.

If it detects that the variable is effectively final, i.e. assigned only once, and never after, everything is good. If it detects that it's not effectively final, it refuses to compile.

So, instead of forcing you to make a variable final, it detects it automatically. But you still can't use non-effectively-final variables inside an inner class or lambda.

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