aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes passing param 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 "passing param" Watch "passing param" New topic
Author

passing param

Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
lass ValHold{
public int i = 30;
}
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);//2
}//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);//3
}//End of another
}
can someboyd give me a detailed explanation of why v.i on line 3 prints 30,while v.i on line 2 prints 20
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Tanuja,
The reason you're seeing the difference is because of the lines:

When these lines are executed in the method another(), the value of the formal parameter v is changed to reference a new object. You now have two distinct ValHold objects. You have the initial one, which you modified to hold a value of 20 and the new one, which is initialized to hold a value of 30.
That's why you see the two different values - you have two different objects.
Corey


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Tanuja,
Here is an exceptionnal second warning about your name!
(first warning here: http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015106 )
Please, read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you for your cooperation.
If you fail to comply with our rules, your account may be closed.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
hi Tanuja
In java when we pass an object reference as a paramter in a method call..the copy of the reference value is actually passed rather than the reference itself..
so in the call another(v.i,i)...we are passing a copy of the reference value(ie. the address stored in the v). to the method.So the actual parameter
(Valhold v) in the method void another(ValHold v, int i) is the copy of the reference value in the formal parmeter in the method call.
so,
in the call another(v.i,i)--v.i=30
in the method
--- we are changing the value at the same location by the statement v.i=20;
so now the actual value has been changed.
but then we are creating another object of valhold v which is not same as the previous one..
ie. ValHold v = new ValHold();
and now v.i=30;
makes another object with it's i= 30.

hence , line 2 prints 20 and 3 prints 30.
I hope this is confusing.
swapna


SCPJ2
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you're still confused about how parameters are passed in Java, you might want to check this out. I made it just to clear up this issue.
HTH,
Corey
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: passing param