• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

passing param

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic