| Author |
Pass-by-Vaule Object reference question
|
John Park
Greenhorn
Joined: Aug 04, 2005
Posts: 26
|
|
Hi, I was glancing over Sierra/Bates Java 2 cert book and came across the following code example explaining pass-by-value semantics: void bar() { Foo f = new Foo(); doStuff(f); } void doStuff(Foo g) { g = new Foo(); } From this code: 1. are g and f both referring to the SAME object? or are they 2 separate instances? 2. if g's object is somehow modified from whatever value f passed to it, will f's object also be modified? I'm confused, because from the code, 2 different instances are created, but since f is passed into g, i'm not sure if that means that both variables are pointing to the SAME object? Thanks in advance for your help. JP
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
|
The reference variable g is a copy of the reference variable f. That's a copy of the reference, not the object. At the time of the method being invoked they are pointing to the same instance. As soon as the first line in doStuff executes a reference to a new and different instance is assigned to the variable g and it now points to a different instance. The first variable, f, is not changed in any way precisely because g is a COPY of f and not a "reference" (in quotes because now we're not talking about a Java reference) to f.
|
 |
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
|
|
Originally posted by Ken Blair: The reference variable g is a copy of the reference variable f. That's a copy of the reference, not the object. At the time of the method being invoked they are pointing to the same instance. As soon as the first line in doStuff executes a reference to a new and different instance is assigned to the variable g and it now points to a different instance. The first variable, f, is not changed in any way precisely because g is a COPY of f and not a "reference" (in quotes because now we're not talking about a Java reference) to f.
For more clarifications check out this thread. Object Pass by Value Vs Reference Hope this helps you to understand better.
|
The Best way to predict your future is to create it
Ankur Sharma
|
 |
John Park
Greenhorn
Joined: Aug 04, 2005
Posts: 26
|
|
Thanks Ken, Ok, I understand that, but let's say the doStuff method was like this: class Foo { int height = 10; void bar() { Foo f = new Foo(); doStuff(f); } doStuff(Foo g) { g.height = g.height + 1; } will the object's height value that f is referring to be 11 also? Thanks, JP
|
 |
Ryan McGuire
Ranch Hand
Joined: Feb 18, 2005
Posts: 944
|
|
Originally posted by John Park: Thanks Ken, Ok, I understand that, but let's say the doStuff method was like this: class Foo { int height = 10; void bar() { Foo f = new Foo(); doStuff(f); } doStuff(Foo g) { g.height = g.height + 1; } will the object's height value that f is referring to be 11 also? Thanks, JP
Yes, because g and f are referring to the same object.
|
 |
John Park
Greenhorn
Joined: Aug 04, 2005
Posts: 26
|
|
cool, thanks for the help. I was a bit confused with pass by values using object references, but i think i got it now.. basically whenever an object reference is passed into another object reference, they will possess the same reference to the SAME object..however, if the new object reference instantiates a new instance, they become referenced to 2 differnt objects..so whatever happens to the new reference..the reference which was orginially passed in will not be affected...? Thanks, JP
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
Originally posted by John Park: cool, thanks for the help. I was a bit confused with pass by values using object references, but i think i got it now.. basically whenever an object reference is passed into another object reference, they will possess the same reference to the SAME object
No. They are two different references to the same object. That is key, "g" is a COPY of "f" -- they aren't the same reference, they just point to the same object.
Originally posted by John Park: ..however, if the new object reference instantiates a new instance, they become referenced to 2 differnt objects..so whatever happens to the new reference..the reference which was orginially passed in will not be affected...? Thanks, JP
Since "g" is not "f", they are two completely different references. The value of f is copied to g when the method is invoked. If in your method you change the value of g, for example through assignment, that has no effect on f.
|
 |
John Park
Greenhorn
Joined: Aug 04, 2005
Posts: 26
|
|
ok, now i'm a bit confused... Ken, with what you said above, what about this example then? class Foo { int height = 10; void bar() { Foo f = new Foo(); doStuff(f); } doStuff(Foo g) { g.height = g.height + 1; } the object that f is pointing to, will also be affected, since f and g both point to the same object....am i missing something here?
|
 |
Kaush Kane
Ranch Hand
Joined: May 22, 2006
Posts: 37
|
|
Hi John, Till tag [1] both the references are pointing to the same object. Hence any changes made to the object pointed by g will also be for the object pointed by f as they point to the same object. After line at tag [2] is executed the reference g starts pointing to the new object and hence any changes made to this new object pointed by g will not affect the object pointed by f.
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
Originally posted by John Park: ok, now i'm a bit confused... Ken, with what you said above, what about this example then? class Foo { int height = 10; void bar() { Foo f = new Foo(); doStuff(f); } doStuff(Foo g) { g.height = g.height + 1; } the object that f is pointing to, will also be affected, since f and g both point to the same object....am i missing something here?
Yes, that's right. Two different references pointing to the same object. Key here is to understand when we're talking about the reference and when we're talking about the object. The object and the reference are two distinct entities here, a reference isn't just an "alias" like in C++, it's completely separate. So a change in one reference's value will not affect a different reference, but a change in the object it points to is going to be seen elsewhere if other references also point to that same object.
|
 |
John Park
Greenhorn
Joined: Aug 04, 2005
Posts: 26
|
|
Gotcha. Thanks to all those who contributed. JP
|
 |
 |
|
|
subject: Pass-by-Vaule Object reference question
|
|
|