"variable is accessed from within inner class needs to be declared final" 错误

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

我在尝试在内部 class 中使用一个 class 的本地成员时遇到此错误。 我知道将其声明为 final 将解决问题,但我读到 Java 8 应该自动处理它,我将 Intellij 与 Java 8 一起使用,但它仍然无法编译。 有没有其他方法可以在不将其声明为 final 的情况下修复它? 谢谢。

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

Java 如果变量是 effectively final.

,8 将处理它

我能想到两种可能的解释:

  1. 也许变量不是有效的最终变量。尝试将其显式声明为 final。如果您遇到编译错误(例如,说某些东西正在尝试修改 final),则该变量实际上不是最终变量。

  2. 也许你没有为 Java 8 正确配置 Intellij 和项目;例如

    • 设置源级别;查看@saman 的回答
    • 检查您是否选择了正确的 JDK:

我认为您应该将语言级别从 "Project Structure" -> "Module" -> "your project name" -> "Source" -> "Language Level" 更改并设置到 8