how to explain the change on a object as a parameter
David chenjl
Greenhorn
Joined: Jul 05, 2001
Posts: 19
posted
0
plz see the following code: --------------------------------------------------- class ValHold{ public int i = 10; } public class ObParm{ public static void main(String argv[]){ ObParm o = new ObParm(); o.amethod(); } public void amethod(){ int i = 99; ValHold v = new ValHold(); v.i=30; another(v,i); System.out.println(v.i + " "+i); }//End of amethod public void another(ValHold v, int i){ i=0; v.i = 20; ValHold vh = new ValHold(); v = vh; System.out.println(v.i+ " "+i); }//End of another } ------------------------------------------- the result is "10 0 20 99" I know the primitive varialbe's change which been send as paramter to a method. but how about a object if it is a paramter in a method,in the up code, i can see that v.i has been change to 20,if have the following code: ValHold vh = new ValHold(); v = vh; the reference i of v will be 20 after the another(). But object v has been assigned to vh, so, v.i should be 10,but compling result is 20, how to explain it? I am not very clear about object and object reference, Please help me. thank you very one.