• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Call by Reference - help

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is from marcus exam2
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);
}//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
}
Answer is 10,0,20
Please expalin me how is the value of instance variable i changing .
Thanks in advance for your expalination
thanks
rohan
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rohan mehta:
this is from marcus exam2
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);
}//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
}
Answer is 10,0,20
Please expalin me how is the value of instance variable i changing .
Thanks in advance for your expalination
thanks
rohan


Hi Rohan,
Just review the method another() one more time:
public void another(ValHold v, int i){
i=0; // 1
v.i = 20; // 2
ValHold vh = new ValHold(); // 3
v = vh; // 4
System.out.println(v.i+ " "+i); // 5 This line yields 10 0
}
First, look at line //3, a new ValHold object is created. That will invoke its constructor, which will set 'i' of 'vh' to 10 (i.e. vh.i = 10)
Second, look at line //4. you copy the address of vh to v via the assignment v = vh. v now has the reference that points to the memory block of the object recently created. Therefore v.i is the same as vh.i (i.e v.i = 10)
Third, look at line //1; i was set to 0, right? Therefore line //5
System.out.println(v.i + " " + i) yields 10 0
Now look at line // 2; that line sets the 'i' value of the object refered by v to the value of 20. Please remember this since it will be crucial for the explanation followed.
---------------------------------------------------
Now let's take a look at amethod():
public void amethod(){
int i = 99;
ValHold v = new ValHold(); // 1
v.i=30; // 2
another(v,i); // 3
System.out.println(v.i); // 4 This line yield 20
}//End of amethod
First, look at line // 3, when you pass v in another(), you pass a copy of v's reference to that method. Such copy will also points to the object created in line // 1. At this point in time v.i = 30 set in line //2.
But did you remember that in the method another() explained above, line //2 of another() change the 'i' value of the object to 20? That is how you got the answer of 20 in line // 4
System.out.println(v.i); // 4
Hope that helps,
Lam

[This message has been edited by Lam Thai (edited April 24, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic