Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

passing parameters..

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following example is a question from Marcus Green's notes..

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
}
I thought the answer would be.. 10, 0 ,30
But, it is given that the answer is 10, 0 ,20.
Please, can anyone give a detailed explanation for this..
thank u
Mary .
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are passing objects, you are passing them by reference. So, you can change the contents of the object using the reference. That's why the assignment v.i = 20; made inside the method 'another' takes effect.
But you cannot change the value of the reference itself, that's why the ValHold object created inside the method 'another' is assigned to only the copy of the reference, but the original object inside the method 'amethod' is not affected.
HTH
------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason for this is that the argument v which is passed into the method another(ValHold v, int i), is a copy of the original reference. So when System.out.println(v.i + " " + i) has printed out 10, 0 you return to the calling method amethod(), and the reference variable v still points towards the original object which contains v.i = 30. So the output will then be 30.
The thing to remember is that you Always pass a copy of a primitive variable or object reference into a method parameter.
I don't know if this was of much help, please reply if you need a more thourough explanation.
Regards
Dominic Steng�rd
Sun Certified Programmer for the Java 2 Platform
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please further explain it, why it prints 20 instead of 30.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void amethod(){
int i = 99;
ValHold v = new ValHold(); //Line 5
v.i=30;
another(v,i); //Line 1
System.out.println(v.i); // Line 2
}//End
public void another(ValHold v, int i){
i=0;
v.i = 20; // Line 3
ValHold vh = new ValHold();
v = vh; //Line 4
System.out.println(v.i+ " "+i);
}//End of another

At line 1, another method is called by passing a copy of reference v.
now, reference v in another method and reference v (of Line 1) both refer to the same object (I say 'OBJECT').
At Line 3, instance variable (i.e., object variable) 'i' referenced by reference v is assigned 20 (and value 30 is overwritten).

(Note : how about if 'another' method is changed like this :

public void another(ValHold v1, int i){ <== changed to v1 from v and used
i=0; <== that in this method.
v1.i = 20; // Line 3
ValHold vh = new ValHold();
v1 = vh; //Line 4
System.out.println(v1.i+ " "+i);
}//End of another

Does that make things more clear (i.e., not to confuse with variable 'v'
as it is used in both methods).

Hope this helps.
 
P Hunjan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your detailed explanation.

If i am not wrong this time.
I can say that as when we pass a copy of reference, we cant change the object but can access its variables & methods & changes to the variables persists even out of the methods.

Thanks
reply
    Bookmark Topic Watch Topic
  • New Topic