• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shadowing Object References?

 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you not omitted something? Such as formatted code between tags?
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Anyone please provide me with reason for the code above?

Regards,
Jothi Shankar Kumar. S
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ October 17, 2006: Message edited by: wise owen ]
 
Ramu Malur S R
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im terribly confused. I did not quite understand.
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic