gpt4 book ai didi

Java 内部类

转载 作者:搜寻专家 更新时间:2023-10-31 20:23:13 24 4
gpt4 key购买 nike

<分区>

Possible Duplicates:
Cannot refer to a non-final variable inside an inner class defined in a different method
Why inner classes require “final” outer instance variables [Java] ?

class MyOuter {    

private String x = "Outer";
void doStuff(){
final String z = "local variable";
class MyInner {
public void seeOuter(){
System.out.println("Outer x is" + x);
System.out.println("Local variable z is" + z); // does
// not compile if final keyword from String z is removed
}
}
}
}

上面的代码工作正常。我想知道为什么如果我从 String z 中删除 final 关键字,编译器会报错。 final 关键字有什么区别?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com