简体   繁体   English

“从内部类访问变量需要声明为final”错误

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

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.我知道将其声明为 final 将解决问题,但我读到 Java 8 应该自动处理它,我在 Java 8 中使用 Intellij,但它仍然无法编译。 Is there any other way to fix it without declare it as final?有没有其他方法可以修复它而不将其声明为最终版本? thanks.谢谢。

I know that declare it as final will solve the issue but I read that Java 8 should handle it automatically.我知道将其声明为 final 将解决问题,但我读到 Java 8 应该自动处理它。

Java 8 will handle it if the variable is effectively final .如果变量实际上是final , Java 8 将处理它。

I can think of two possible explanations:我能想到两种可能的解释:

  1. Maybe the variable is not effectively final .也许变量不是有效的 final Try explicitly declaring it as final .尝试将其明确声明为final If you get a compilation error (eg saying that something is trying to modify the final ) then the variable isn't effectively final.如果您收到编译错误(例如,说某事正在尝试修改final ),那么该变量实际上不是最终的。

  2. Maybe you haven't configured Intellij and the project properly for Java 8;也许您没有为 Java 8 正确配置 Intellij 和项目; eg例如

I think you should change your language level from "Project Structure" -> "Module" -> "your project name" -> "Source" -> "Language Level" and set it to 8我认为您应该从“项目结构”->“模块”->“您的项目名称”->“来源”->“语言级别”更改您的语言级别并将其设置为8

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从内部类访问变量“ ”,需要声明为 final - Variable ' ' is accessed from within inner class, needs to be declared final 从内部 class 访问的变量需要声明为 final - Variable accessed from within inner class needs to be declared final 变量(dialogView)是从内部类中访问的,需要声明为final - Variable (dialogView) is accessed from within inner class, needs to be declared final 从内部类访问变量“i”,需要声明为 final - Variable 'i' is accessed from within inner class, needs to be declared final 变量'btnsave'是从内部类中访问的,需要声明为final - Variable 'btnsave' is accessed from within inner class, needs to be declared final 自定义适配器中的“从内部类内部访问变量,需要声明为最终变量”错误 - “Variable is accessed from within inner class, needs to be declared final” error in custom adapter 错误:从内部类访问局部变量 imagesLabel; 需要声明为 final (Java) - Error: local variable imagesLabel is accessed from within inner class; needs to be declared final (Java) 错误:从内部类中访问局部变量事件; 需要宣布最终 - Error: local variable event is accessed from within inner class; needs to be declared final 错误:从内部类访问局部变量 a; 需要宣布为最终 - Error: local variable a is accessed from within inner class; needs to be declared final 在内部类中访问变量。 需要宣布为最终 - Variable is accessed within inner class. Needs to be declared final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM