This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clarification regarding shadowing of variables

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I tried out the following code snippet:



the output is

sh.myBar.barNum value before changeIt is 28
Value in changeIt is 99
Value in changeIt is now 420
sh.myBar.barNum value after changeIt is 99

I have a few queries:

a) What is meant when I say sh.myBar.barNum? What is sh.myBar referring to?

b) Why does the last line return value 99?
 
Tony Docherty
Bartender
Posts: 2989
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) What is meant when I say sh.myBar.barNum? What is sh.myBar referring to?

Ask yourself what sh refers to. Then look in that class definition to see if there is an instance variable called myBar and see what type that is.

b) Why does the last line return value 99?

Because the changeIt() method creates a new Bar object which it assigns to a local variable called myBar and which it sets to 420m but this doesn't replace the Bar object referenced by the instance variable myBar.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
a) What is meant when I say sh.myBar.barNum? What is sh.myBar referring to?

Ask yourself what sh refers to. Then look in that class definition to see if there is an instance variable called myBar and see what type that is.

b) Why does the last line return value 99?

Because the changeIt() method creates a new Bar object which it assigns to a local variable called myBar and which it sets to 420m but this doesn't replace the Bar object referenced by the instance variable myBar.


So when I say myBar.barNum =99, I am essentially changing the value of the sh.myBar variable that was passed as a parameter to changeIt method. Correct?
 
Tony Docherty
Bartender
Posts: 2989
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when I say myBar.barNum =99, I am essentially changing the value of the sh.myBar variable that was passed as a parameter to changeIt method. Correct?

Not quite, you are setting value of the instance variable called barNum in the object referenced by myBar to 99. At the start of the method the myBar local variable references the sh.myBar object.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
So when I say myBar.barNum =99, I am essentially changing the value of the sh.myBar variable that was passed as a parameter to changeIt method. Correct?

Not quite, you are setting value of the instance variable called barNum in the object referenced by myBar to 99. At the start of the method the myBar local variable references the sh.myBar object.


Ohhhh.. Ok. Thanks Tony. I just realized that writing down the objects and their references and instance variables makes picture much more clear. Form now I will write down rather that orally trying to solve such tricky ones.
 
Tony Docherty
Bartender
Posts: 2989
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Form now I will write down rather that orally trying to solve such tricky ones.

If you are confused then writing it out should help. I think you will find that once you have written out a few of these it will become easier to just read and understand them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic