【问题标题】:Inner Local Classes in JavaJava中的内部局部类
【发布时间】:2015-02-08 23:12:58
【问题描述】:
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 或引用。它运行了,我没有收到任何错误。如何?有人可以解释这种行为吗?

【问题讨论】:

标签: java local-class


【解决方案1】:

你的书可能已经过时了。从 Java 8 开始,您可以使用 effectively final 局部变量。

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

【讨论】:

  • 感谢您的回答。还有,我如何关注有关 java 更新的消息?
  • @Furkan This 是 Oracle 官方页面。
猜你喜欢
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2021-11-02
  • 1970-01-01
相关资源
最近更新 更多