关于方法内的内部类使用方法参数的问题
如以下代码片段:
public class Outer{
private int m;
public void go(int x, final int y){
int a;
final int b;
class Inner {
public void method() {
System.out.println("m="+m);
System.out.println("x="+x); //不合法
System.out.println("y="+y);
System.out.println("a="+a); //不合法
System.out.println("b="+b);
}
}
}
关于为什么使用final关键字的解释有如下几种观点:
1.因为final变量是一个编译时常量,而内部类在编译时要确定所有变量的值,所以只能使用编译时常量,即要使用final变量。
2.《corejava》中解释:Thus, it is guaranteed that the local variable and the copy that is made inside the local class always have the same value.
3.要确此变量在内部类的使用过程中不改变引用值。
以上是从各处收集的解释,我认为都不完全正确,请大家讨论一下。