• Post Reply Bookmark Topic Watch Topic
  • New Topic

please explain me parameter passing  RSS feed

 
pias Khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain me parameter passingfor the example below:

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.print( v.i );
}
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}
}
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call the another() method you are passing a copy of your reference to the original object, rather than the actual reference variable. Thus, when you do v=vh in your method you are only pointing the local method variable (declared in the method signature) to point to the new object.

Compare this with the following code:


Again, the parameter will be a copy of the int passed to the method, rather than the actual int variable, so the value of the variable in main() will not be changed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!