This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help: Object references as method parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help: Object references as method parameters" Watch "Help: Object references as method parameters" New topic

Help: Object references as method parameters

Ruff Young
Ranch Hand

Joined: Sep 26, 2002
Posts: 513
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?

SCJP2, SCWCD<br />IBM Certified WebSphere<br />IBM Certified Solution Developer for WSAD
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
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.
Kathy (still getting used to life back at the ranch, after being a city girl for much too long).

Co-Author of Head First Design Patterns
Just a Jini girl living in a J2EE world.
Ruff Young
Ranch Hand

Joined: Sep 26, 2002
Posts: 513
Thank you for your help.
I agree. Here's the link:
subject: Help: Object references as method parameters
It's not a secret anymore!