1
public class Main {
    public static void main(String[] args) {
        int b=1;
        final int c=2; 
        String s1[] = new String[]{"A","B","C"};

        class InnerMain{                                    
            int a=5;

            public void show(){ 
                System.out.println(s1[0]);
                System.out.println("A:" + a);
                System.out.println("B:" + b);
                System.out.println("C:" + c);
            }
        }

        InnerMain test =new InnerMain();
        test.show();
    }
}

我研究过的书说,本地类只能使用final本地类所在的方法的变量和引用。在这个例子中,我使用b了不是final或引用的变量。它运行了,我没有收到任何错误。如何?有人可以解释这种行为吗?

4

1 回答 1

5

你的书可能已经过时了。从 Java 8 开始,您可以有效地使用最终局部变量。

如果您尝试b在本地类定义之前、之后或中的任何地方进行更改,您将收到编译器错误。

于 2015-02-08T23:14:24.760 回答