• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

shadowing

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this is the code from k&S .i am not able to understand how variables/objects behave in case of shadowing.
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


well lets start from line 12. AS there is a new object of Foo is created the value of barNum for that object is '0' .
then we have which passes the bar object as a reference.
In line 5 it prints the value of 99 as the value of barNum is changed to 99.
In line 6 a new object of Bar is created and thus the reference to the previous bar is lost for that variable, which now points to a new object.
In line 8 420 is printed since that is the value for the new bar object.

Now when we return to line 14 the value for barNum is going to be 99 since that is the last time the value has been changed for that object.

The object passed as an argument is shadowed by the creation of a new object in the method.



 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming the Bar Object somewhere has a public variable barNum?

 
Ravikanth kolli
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yap that is what i assumed..
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running the code with my own implementation of Bar:



I get the folowing output:



May I be so bold as to ask exactly what this has to do with shadowing?

 
Ravikanth kolli
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i think that creation of a new object in the method has actually shadowed the object that is passed as an argument. dont you think so?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is just an example of shadowing an instance variable by declaring a local variable of the same name and the shadowed variable is an object reference. The local variable myBar in changeIt() method is affecting myBar instance variable that is referring to an object of type Bar. myBar parameter receives reference to same Bar object.

 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I see now, the Instance myBar object reference is shadowed by the myBar Bar reference of the argument to the changeIt() variable (local variable) So when we are acting upon the myInt variable we are actually acting upon the local variable, but it can seem as if we are working on the instance variable.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"ZooKeeper" topic turns out to be an instance of shadowing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic