aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to explain the change on a object as a parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to explain the change on a object as a parameter" Watch "how to explain the change on a object as a parameter" New topic
Author

how to explain the change on a object as a parameter

David chenjl
Greenhorn

Joined: Jul 05, 2001
Posts: 19
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

Joined: Apr 11, 2001
Posts: 356
Hi,
Similar question has been discussed here, take a look at it. http://www.javaranch.com/ubb/Forum24/HTML/010715.html
Vanitha.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to explain the change on a object as a parameter