The following is an example from the K&B book on page 208.
This is regarding shadowing of object references. I fail to understand why the myBar variable declared in the changeIt method receives a reference to the same bar object? What this actually implies. Does it mean that shadowing does not apply when it comes to object references?
Regards, Jothi Shankar Kumar. S
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Whenever a variable (be it a reference or a primitive) is assigned to another, what gets assigned is a value stored in the variable.
In case of primitives, this is the actual value, whereas for references, this is a "way" to get to the object on heap.
When changeIt() is called, the argument value to changeIt(), which indicates how you can get to the object, is stored in mybar (the parameter of changeIt()).
When num is set to 99, both the mybar references refer to the same object. Hence, the original object is changed.
But later on in changeIt(), mybar (parameter) is made to point to a new object. Now what is available within the scope of changeIt() is the instance member mybar pointing to the original object, and a local variable mybar pointing to a new object. Here is where shadowing takes place, and you end up using the parameter of changeIt() to refer to the new object (unless you use this.mybar). [ October 17, 2006: Message edited by: Aniket Patil ]
SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!