“variable is accessed from within inner class needs to be declared final” error

放肆的年华 提交于 2019-12-31 07:00:08

问题


I've got this error while trying to use a local member of one class in inner class inside. I know that declare it as final will solve the issue but I read that Java 8 should handle it automaticlly, I'm using Intellij with Java 8 and it still does not compile. Is there any other way to fix it without declare it as final? thanks.


回答1:


I know that declare it as final will solve the issue but I read that Java 8 should handle it automatically.

Java 8 will handle it if the variable is effectively final.

I can think of two possible explanations:

  1. Maybe the variable is not effectively final. Try explicitly declaring it as final. If you get a compilation error (e.g. saying that something is trying to modify the final) then the variable isn't effectively final.

  2. Maybe you haven't configured Intellij and the project properly for Java 8; e.g.

    • Set the source level; see @saman's answer
    • Check you have the right JDK selected: Java 8 doesn't compile on Intellij15



回答2:


I think you should change your language level from "Project Structure" -> "Module" -> "your project name" -> "Source" -> "Language Level" and set it to 8



来源:https://stackoverflow.com/questions/37758034/variable-is-accessed-from-within-inner-class-needs-to-be-declared-final-error

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