• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help: Object references as method parameters

 
Ruff Young
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code what will be the output?
1 class ValHold{
2 public int i = 10;
3 }
4 public class ObParm{
5 public static void main(String argv[]){
6 ObParm o = new ObParm();
7 o.amethod();
8 }
9 public void amethod(){
10 int i = 99;
11 ValHold v = new ValHold();
12 v.i=30;
13 another(v,i);
14 System.out.println(v.i);
15 }//End of amethod
16 public void another(ValHold v, int i){
17 i=0;
18 v.i = 20;
19 ValHold vh = new ValHold();
20 v = vh;
21 System.out.println(v.i+ " "+i);
22 }//End of another
23 }
After calling another method, how does "v.i" still have v.i = 20?
Could you anybody explain it?
Thanks
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is that there are *two* references named "v" -- the one created on line 11, and then one created on line 16 -- the local parameter "v" declared as part of the of the "another" method argument.
Look at line 20 -- it is assigning the new ValHold object (referenced by "vh") to the local parameter 'v', but not making any changes to the first "v" (the one from line 11). So when the *another* method completes, the local "v" is completely out of scope (although on line 18 it did of course alter the state of the object referenced by the "v" from line 11), and so on line 14 we're using the object referenced by the first "v".
In summary:
* We have two "v" variables and two ValHold objects
* The first "v" is created on line 11, the second is created on line16.
* On line 18 we change the value of the original ValHold object (referenced by the first 'v") so that its "i" is 20.
* On line 20 we are reassigning the second "v" (the one local to the *another* method) to reference a *different* ValHold object
* But by the time we get back to line 14, the final println, we're still using the original, *first* ValHold object.
Remember, when you pass a reference into an object, it is only a *copy* of the original reference. The copy gets you to the same object referenced by the one you passed in, but the method CALLED cannot change the reference of the CALLER. The called method can change the OBJECT, but not the reference itself. In other words, a different method cannot ever take the original reference passed into the method and assign it to something else. They can't change what the passed-in reference refers to. Once it gets passed in, the method it gets passed to is only working on a copy, not the original, reference.
Cheers,
Kathy (still getting used to life back at the ranch, after being a city girl for much too long).
 
Ruff Young
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic