aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shadowing Object References? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

Shadowing Object References?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Ranchers,

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!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Have you not omitted something? Such as formatted code between tags?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Barry,

Sorry...I have forgotten to put the code in place. So here goes below,



My question is why object references are not being shadowed unline primitives?

Regards,
Jothi Shankar Kumar. S
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Ranchers,

Anyone please provide me with reason for the code above?

Regards,
Jothi Shankar Kumar. S
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

[ October 17, 2006: Message edited by: wise owen ]
Ramu Malur S R
Greenhorn

Joined: Oct 17, 2006
Posts: 26
Hi Jothi,

The reference myBar in changeIt(Bar myBar) is a copy of the reference f.myBar declared in main(), not the myBar member variable(which is this.myBar).

Thus the new object created is assigned to the reference copy. But the reference in main still refers to the same original object.

- Ramu
"Excellency is rarely found, more rarely valued"


"Excellency is rarely found, more rarely valued"
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Im terribly confused. I did not quite understand.
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
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!
 
Consider Paul's rocket mass heater.
 
subject: Shadowing Object References?