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也照样运行,求解! |
|