首先,在another方法中,
有一句v.i=20;这句修改的是v实例所指的内存空间中的i的内容,
后一句v=vh,把v指向vh的内存地址,
所以,System.out.println(v.i+""+i);是10 0,
但是,由于v是对象,所以,当除了这个方法以后,
它所指的内存地址是不变的,
还是没有进方法前的地址,
所以v.i是20;
对象本身不能变,
但是对象所指的内容可以改变。

解决方案 »

  1.   

    天水蓝,每次看到你都是guanzu两个字,不会讲点别的啊兄台?
      

  2.   

    stendhal(stendhal)你说:"对象本身不能变,但是对象所指的内容可以改变。"对象所指的内容改变了,你在访问这个对象 ,肯定会被它指到别的地方啊?
    还是不太理解哦
      

  3.   

    在v = vh;行其实你可以理解为 v = new ValHold();这样的话another()中的v就和amothod()
    中的v不一样了。
      

  4.   

    stendhal(stendhal)说得对,在将对象作为参数传入方法时,对象的地址是不能变的,能变的是对象的内容,当方法结束时,参数回复本来的地址。
    v =  vh;实际上只是生成一个临时对象,
    public void another(ValHold v, int i)
      {
        i=0;
        v.i = 20;
        ValHold vh = new ValHold();
        v =  vh;
        System.out.println(v.i+ " "+i);
      }
    实际上是
    public void another(ValHold v, int i)
      {
        i=0;
        v.i = 20;
        ValHold vh = new ValHold();
        ValHold v =  vh;
        System.out.println(v.i+ " "+i);
      }
      

  5.   

    class SL275
    {
      public static void main(String argv[])
      {
        SL275 o = new SL275();
        o.amethod();
      }
      public void amethod()
      {
        int i = 99;
        ValHold v = new ValHold();
        v.i=30; //change the variable(i) value to 30
        another(v,i); //this statement set v.i to 20
        System.out.println(v.i);//关键问题在这句话
      }
      public void another(ValHold v, int i)
      {
        i=0;
        v.i = 20; //change variable value to 20. It's in the origin instance created in amethod.
        ValHold vh = new ValHold(); //create a new instance, differ from the one created in amethod()
        v =  vh; //point to a new instance, it's different from the origin one now.
        System.out.println(v.i+ " "+i); //print10 0
      }
    }