• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass by reference/Value

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I was expecting 10 0 10 as answer for this.

But this code is giving 10 0 20 as output. Please explain me about this.

===============================================



==================================
Thanks in advance,
Mubeen Shaik
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two objects of class ValHold1.

When each object is created, member variable i is initialized to 10.

Method amethod() sets its own local variable i to 99. It then creates the first ValHold1 object and changes that object's member variable i from 0 to 30. amethod() then calls method another() with two arguments, a reference to the first ValHold1 object and the value 99 from amethod's local variable i. These are copied from amethod()'s local reference variable v and amethod()'s local int variable i.

Method another() gets two parameters, the reference to the first ValHold1 object and the int value 99.

Formal parameters are pretty much like local variables, so another() can now change its formal parameter i from 99 to 0 without affecting amethod's local variable i, which stays at 99.

another() then changes the first ValHold1 object's member variable i from 30 to 20. It then creates the second ValHold1 object and stores it's reference in vh. The second Valhold1 object's member variable i is automatically initialized to 10.

another() then changes parameter v from referencing the first ValHold1 object to referencing the second ValHold1 object [v=vh;]. This does not affect the local reference variable v in amethod().

another() then uses parameter v to access the second ValHold1 object's member variable i, which is still 10, and it prints the 10, a space, and another()'s parameter i, which another() had changed to 0.

Finally, another() returns to amethod(), which uses it local reference variable v to access the first ValHold1 object's member variable i, which another() had changed to 20. The 20 is printed and we are done.

Of course, most of the confusion was deliberately created by having two member variables, one local variable, and one formal parameter, all named i.
[ December 14, 2004: Message edited by: Mike Gershman ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first method, we use "new" to create an object of the type ValHold1, referenced by the local variable v.

When we call another(v, i), we are passing a reference to that object. Method arguments are copies, and they are local to the method. So we now have a local variable v in the first method, and another local variable v in the second method. But although these are separate local variables, they both reference the same object (the one created with "new" above).

So when we invoke v.i=20 (using the reference copy in the second method), this sets i=20 in the object itself. So if the first method were to reference that object (with its own local variable v), the value of v.i will be 20.

After modifying the original instance of ValHold1, the second method also creates a new instance of ValHold1, referenced by vh. Then, it sets v=vh. This simply reassigns the local variable v in the second method, so that it references the newly created object instead of the original object.

Note, however, that this has no effect on the first method's local variable v, which still references the original object.

So... When the second method prints v.i, it is printing the value of i in the newly created instance (also referenced by vh), so this is 10. When the second method prints i, it prints the value of the local variable i, so this is 0. Then when the first method prints v.i, it is printing the value of i in the original instance, which is now 20 because the object itself was modified (by way of the reference passed to the second method).
[ December 14, 2004: Message edited by: marc weber ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike wrote: "Of course, most of the confusion was deliberately created by having two member variables, one local variable, and one formal parameter, all named i."

Mubeen, once you realize that this is possible, it can help you understand what is going on if you rewrite the code using different names in the respective scopes.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and always remember that Java is purely pass by value.
You can never change a reference variable passed into a method, only fields on the object that reference refers to.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
...Java is purely pass by value.


Yes, "passing a reference" in Java is actually passing a copy of the reference. A value by any other name...
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In cases like this and in cases of garbage collection I find it helps a lot to draw a picture to illustrate what's going on. For ValueHolder, you would have one object with two references to it. The object is then changed, and then one of the references changes to point to a different object.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic