java代码攻关知识点

#final的使用

class Something{
public static final Other o=new Other();
         public static void main(String[] args){
                          new Something().addOne(o);
//o=new Other(); wrong final cant revalue 报错,这时修改的是类的o;
         }
         public void addOne( Other o){
                   o.i++;//2
                   o=new Other(i);//正确因为此时o与类中的o为不同的对象,有不同的内存空间。
                   //this.o =  new Other(1);此时会报错因为this.o代表的是该类的o所以违背了o是静态常量的规则。
         }
}
class Other{
Other(int i){
this.i=i;
}
Other()
{}
         public int i;
}

猜你喜欢

转载自blog.csdn.net/weixin_44627672/article/details/105582106