my dog learned polymorphism*
The moose likes Beginning Java and the fly likes clarification regarding shadowing of variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "clarification regarding shadowing of variables" Watch "clarification regarding shadowing of variables" New topic
Author

clarification regarding shadowing of variables

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Aug 07, 2007
Posts: 2173
    
  47
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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Aug 07, 2007
Posts: 2173
    
  47
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.
 
 
subject: clarification regarding shadowing of variables
 
Similar Threads
scjp chapter3 2nd example of page 207-208
Shadowing of reference variables
shadowing an object
shadowing
shadowing object reference variables