• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by val/ref ?

 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
The code above executes to give the following answers - 10,0,20. 10 and 0 are understandable but why 20. Could somebody please clarify!
 
Ajay Kumar
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur,
Please refer to the following link for explanation.
http://www.javaranch.com/ubb/Forum24/HTML/001293.html
regds
Ajay K
[This message has been edited by Ajay Kumar (edited July 06, 2000).]
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ajay!!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic