A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

public class TestFinal
{
    //这样定义 User user 代码顺利运行了,不是需要final么
    public void test( User user)
    {
        (new Thread()
            {
                public void run()
                    {   
                        System.out.println("user.name-->"+user.name);
                    }
            }
        ).start();  
    }
    public static void main(String[] args)
    {
        User user=new User();
        user.setId(007);
        user.setName("zhaoyang");
         
        TestFinal testFinal=new TestFinal();
        testFinal.test(user);   
    }
}


class  User
{
    String name=null;
    int id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
     
}

按照道理上面User user应该定义成final User user,但是没有final代码照样运行,不报错。


public class Parcel10 {

//这个地方不加final也照样不报错啊
public Destination destination(String dest, float price) {
return new Destination() {

private float cost;
{
cost =Math.round(price);
if(cost>100)
{
System.out.println("over budget");
}
}
private String label = dest;

@Override
public String readLabel() {
return label;
}

};
}

public static void main(String args[]) {
Parcel10 par = new Parcel10();
par.destination("taiwang", 109.4f).readLabel();
}


}


我记得是要final  , 但是上面不加final也照样运行,求解!

10 个回复

正序浏览
iamnoten 发表于 2015-5-17 23:09
这的吗?还不知道有这功能

有很大的改动么
回复 使用道具 举报
好高级,学习了
回复 使用道具 举报
1.8以后,并非不用是final的,而是在编译期间要求值不发生变化。在你的代码中,如果user的值变化了,就会出错。
编译如下代码:
  1. public class TestFinal
  2. {
  3.     //这样代码就不能通过编译了
  4.     public void test( User user)
  5.     {
  6.         user = new User();
  7.         user.setName("zhaoyang");
  8.         (new Thread()
  9.             {
  10.                 public void run()
  11.                     {   
  12.                         System.out.println("user.name-->"+user.name);
  13.                     }
  14.             }
  15.         ).start();  
  16.     }
  17.     public static void main(String[] args)
  18.     {
  19.         User user=new User();
  20.         user.setId(007);
  21.         user.setName("zhaoyang");
  22.          
  23.         TestFinal testFinal=new TestFinal();
  24.         testFinal.test(user);   
  25.     }
  26. }


  27. class  User
  28. {
  29.     String name=null;
  30.     int id;
  31.     public String getName() {
  32.         return name;
  33.     }
  34.     public void setName(String name) {
  35.         this.name = name;
  36.     }
  37.     public int getId() {
  38.         return id;
  39.     }
  40.     public void setId(int id) {
  41.         this.id = id;
  42.     }
  43.      
  44. }
复制代码

你会得到类似下面的错误:
TestFinal.java:12: 错误: 从内部类引用的本地变量必须是最终变量或实际上的最终变量
                        System.out.println("user.name-->"+user.name);
回复 使用道具 举报
新版本就不用了吧。。。。。
回复 使用道具 举报
这题目有点没看懂,难道不是因为线程一直在跑 所以匿名对象不会消亡?
回复 使用道具 举报
灞波儿奔 发表于 2015-5-17 23:06
1.8版本以后就不用了。

谢谢 ,我知道了
回复 使用道具 举报
大多数都没用到1.8  现在学习最好加上,并且知道为什么要加。可以百度【 内部类 final 生命周期】得到答案
回复 使用道具 举报
我的也是。我理解为现在改进了,可以不加final了。
回复 使用道具 举报
灞波儿奔 发表于 2015-5-17 23:06
1.8版本以后就不用了。

这的吗?还不知道有这功能
回复 使用道具 举报
          1.8版本以后就不用了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马