• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Shadowing Object References?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you not omitted something? Such as formatted code between tags?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Anyone please provide me with reason for the code above?

Regards,
Jothi Shankar Kumar. S
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ October 17, 2006: Message edited by: wise owen ]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im terribly confused. I did not quite understand.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic