final修饰的变量的特性:
1.final修饰的地址值不能改变
public class Test{
public static void main(String[] args){
final Other o = new Other();
o = new Other(); //会报错 final修饰的引用地址不能变
}
}
public void addOne(Other o){
o.i = 20;
o = new Other(); //不会报错
//因为形参Other other是addOne()方法的变量,这个other只是用来接收方法调用处传递的实参,换句话说,他就是用来保存传过来的 o 的地址值,而形参的Other并没有使用final修饰,所以不会报错。
}
public void addOne(final Other o){
o.i = 20; //属性的值可以改变
o = new Other(); //会报错 被final修饰了所以引用不能变
}