wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing References Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing References" Watch "Passing References" New topic
Author

Passing References

Ankitt Dhebarr
Ranch Hand

Joined: Feb 13, 2009
Posts: 32


Can anyone please help me for this code !!
Can anyone explain this to me step by step.
It has been taken from K&B self test

Thanks in advance !
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

The first (f1==f3) returns true. This is because FizzSwitch is given the value f1,the reference variable value that points to Fizz object. Then reference variable z is given the value contained in x. So now z reference variable points to the same object pointed by f1(JAVA IS PASS-BY-VALUE). This value is returned and stored in f3. So now f3 points to the same object that as f1. Hence f1==f3 returns true. Then f1.x , f3.x has 6 as value as f1 and f3 points to the same object, hence true.

Hope this helps.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ankitt Dhebarr
Ranch Hand

Joined: Feb 13, 2009
Posts: 32

great sudipto thanks a lot for your help !!
but please correct me wherever I am wrong:
if we are setting z.x=6; ( x is the method parameter and not the variable declared with the value 5 ?? Am i right ?
and if we are setting the z value to 6 i.e f3's value so then f1 will be automatically changed. Am i right ?? because If one reference is changed the the same gets reflected into the other one also !
Am I right ???
Please tell me !!
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45




No x is the instance variable declared at line 2. Above its value is getting changed for object refrenced by z,x,f1 and later by f3.

Regards
Sunny Mattas
SCJP5


Regards
Sunny Mattas
SCJP5
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

As f1 and f3 refers to the same object, the change made through z.x or f3.x we are indeed changing the value of the instance variable x that is referred also by f1. When you do Fizz f3 = FizzSwitch(f1,f2); you are passing the value contained in the container f1 to x and that same value is returned to f3,where the value is a path to reach to the object, hence the phenomenon seen.

And also static Fizz FizzSwitch(Fizz x, Fizz y) x is a reference variable of type Fizz and z.x refers to the instance variable x of the class Fizz.

Hope this helps
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Here is my take on the code with some explanatory comments which should explain:



be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Stephen,

Actually, z.x is not a local variable. It will actually refer to f1.x, since z points to the same object as f1 does. So the values of f1.x and f3.x will both be 6.


All code in my posts, unless a source is explicitly mentioned, is my own.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Ruben, is that because, the f1 is a final reference? Take a look at the comments and code, am I on the right track here?




Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Stephen, your code and comments are right. But the fact that f1 or z are final is irrelevant. When you mark a reference variable as final it just means that you can't reassign it once it has been initialized. I hope that makes sense.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Yes I think I understand. I cannot do this for example;



or



However, the one issue I'm trying to comprehend better is that if Java is pass by value (ie copy) how then if we are passing f1 which is a reference to the method, able to then change the object it points to - My question is is it because we are simply creating a copy of the remote control in the method and then using that to change x? If this is the case then I understand!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Stephen,

Java is pass-by-value. When you pass a reference variable to a method, the value stored in that variable is copied to the corresponding method's parameter. But in the case of a reference variable, the value is simply a bit pattern which acts as a pointer for a specific object (or the bit pattern for null.) So in the end, both the original reference variable used as an argument in the method call and the method parameter point to the same object. This is a little confusing if you are used to C++ pass-by-value and pass-by-reference, because the semantics of variables in C++ is different than in Java. But if you don't know C++, please ignore what I just said. I hope that helps.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
That's got it, yes I believe it affirms my analogy (Stolen from Bert and Kathy's Head First) of a new remote control to the same object. Thank Ruben, thats cleared it up once and for all!

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Glad to help Stephen. I kept wondering what you meant by "remote" but now I see where you got it from.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing References
 
Similar Threads
Chapter 3 question 5 page 279
Use of final keyword with class object.
Need help
chapter 3 self test question 5, K&B
Final