aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reference passing Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "reference passing" Watch "reference passing" New topic
Author

reference passing

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287


I don't understand why System.out.print(v.i); in the 'another' method is printing value of 10 and in the 'amethod' method is printing value of 20. Can someone please explain to me? Thanks.


SCJA, SCJP5.0, SCBCD, SCWCD
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
inside amethod
---------------

1] v ----> 10

v.i = 30 ;

2] v ----> 30

another(v,i) ;

3] call is made to method where the refernce of the object v and value of variable i
is passed

inside another
--------------

v.i = 20;

1] v ----> 20

the state of the object v is changed ( ie. the value of instance variable i )

ValHold vh = new ValHold();

2] vh -----> 10 ( new object vh is created )

v = vh;

3] v ----> 10 ( this is the copied reference of v and not the object v that is
inside method amethod )
vh ----> 10

now we again return to the method amethod

inside amethod
--------------

the original refernce of the object v still remains the same as was before the call
, but the state has changed because of bthe assignment that had taken place in the
method another
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
The value printed in another is 10.
If you see the code

you will understand. See here we are setting the reference of the ValHold object vh to v,and since both are same type, they both now refer to the same object. So if you access the variable i, which has the value 10, the result will be printed as 10.

But in the other case, where you are passsing the object itself to another method in the code like

the reference is passed. So when you change the value v.i=20 in another method, that value is assigned to v.i now and the value 30 is erased. Also objects are passed by reference, when you are printing the value in amethod you get 20.
Hope it helps
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Originally posted by vjy chin:

the reference is passed. So when you change the value v.i=20 in another method, that value is assigned to v.i now and the value 30 is erased.
Hope it helps


This is true, but AFTER this, v is assigned to a new ValHold instance, whose variable i is 10:

ValHold vh = new ValHold();
v = vh;

So 10 is the latest value i holds, isn't it? Then why after 'another' method finishs, i is 20? Would this mean v in 'amethod' is different from v in 'another' method? We should say Java is pass-by-value for any type data, but we often explain things in pass-by-reference, this also confuses me.

I'm still haven't clear this thought. Someone please explain more. Thanks.
Pramod Chris
Greenhorn

Joined: Oct 15, 2004
Posts: 21
In the method another, the state of the object pointed to by
v is changed. ie., i is now 20. By doing
v = vh;
the reference v points to the object created at
ValHold vh = new ValHold();
so v.i is now 10.
Since Java passes parameters strictly by value, once back in the method amethod, v would not point to the object that vh is pointing to, anymore.
But since you had passed the object reference v into another(), where its state was changed, back in amethod it still points to the very same object whose state was changed. Hence it still prints 20.
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Thanks, Pramod. I have it cleared now.
 
I agree. Here's the link: http://jrebel.com/download
 
subject: reference passing
 
Similar Threads
weird call by ref question
Question on References
which values will be printed?
instance variable
Can someone explain!!!