• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to explain the change on a object as a parameter

 
David chenjl
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Similar question has been discussed here, take a look at it. http://www.javaranch.com/ubb/Forum24/HTML/010715.html
Vanitha.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic