| 因为final定义的局部变量相当于是一个常量,它的生命周期超出方法运行的生命周期。 这个问题我记得回答过了。那个论坛帖子都有的
 class InOut{
 String str = new String("123");
 public void method()
 {
 int i =2;
 final int a =1;
 //System.out.println(a);
 class Inner
 {
 public void sayHello()
 {
 // Cannot refer to a non-final variable i inside an inner class defined in a different method
 //System.out.println(i);
 System.out.println(a);
 }//end of Inner class
 }
 }//end of method
 }
 在内部类中的sayHello方法。我们可以访问变量a,但是不能访问变量i
 因为局部变量和对象的生命周期不一样。局部变量存于栈中,用完就释放。但是如果局部变量被内部类对象调用,调用完,局部释放。但是对象在内存中还存在(只是对象的引用没了),如果对象在再次调用该变量,就找不到了。所以要final.
 
 是不是JDK1.8特性我不清楚。但是我知道如果内部类要调用必须要加final。不调用怎么会不可以定义呢。请大家注意说法
 |