aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shadowing of Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shadowing of Variables" Watch "Shadowing of Variables" New topic
Author

Shadowing of Variables

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661

The output is
28
99
420
99


Can someone explain what exactly is going on and also why we are using f.myBar.barNum.
Why can't we use directly myBar.barNum.

What's the local reference variable and what's the instance reference variable.
What's happening when a new variable is being assigned ie. myBar=new Bar()

I am getting all muddled up


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
Hi Nabila,

myBar.barNum can't be used directly because the code is executed inside a static method main(). There is no instance of Foo available, so to use the instance variable myBar you have to create an instance of Foo first. The line

Foo f=new Foo();
creates an instance of Foo named f and now you have an variable myBar inside of this new instance of Foo, which can be accessed via f.myBar.

Local reference variable is a reference variable declared inside of a method or a parameter list. Example
int testMethod(Foo d) {
Foo x;
}

x is a local reference variable declared inside of method and d is a local reference variable declared over a parameter list of method testMethod().

Instance reference variable is a reference variable declared inside of a class. Example
class Foo{
String s;
}

Now back to your example. The line
Bar myBar=new Bar(); declares and initializes an instance variable myBar. The method void changeIt(Bar myBar) declares a local reference variable myBar over its parameter list. Inside of method changeIt() you have two variables with the same name: a local variable myBar and a instance variable myBar. Such sutiation is called shadowing, because the local variable myBar shadow the instance variable myBar. Inside the method if you use the variable called myBar you always use the local variable and never the instance variable.

With the call f.changeIt(f.myBar) the method local reference myBar is initalized with the value of instance variable f.myBar. Over this reference you change the barNum, which resides inside of f.myBar and prints it out.
With the line myBar=new Bar(); you create an new Object Bar and let the local reference myBar refers to it. Remember you operate with a copy of the instance reference variable f.myBar and if you modiy a copy the orinal instance variable f.myBar will not be modified. So after the method changeIt() is completed the f.myBar referes to the same object as before the call. So the barNum has the value 99, last altered in the line myBar.barNum=99; over a copy of the reference f.myBar.

It was a really long message. I hope it was also helpful.

regards

Serg


SCJP 6.0 [95%] OCP EJBD 6.0 [93%]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hi Serg,
Thank you so much for your reply, you were the only one

I got some part of it,But it's still not very clear.



With the call f.changeIt(f.myBar) the method local reference myBar is initalized with the value of instance variable f.myBar. Over this reference you change the barNum, which resides inside of f.myBar and prints it out.


Why is the local reference variable being initialised with the value of instance variable.Does that alwayas happen, the local reference being initialised with the instance value.
And can you actually modify the value of num via the local reference.

With the line myBar=new Bar(); you create an new Object Bar and let the local reference myBar refers to it.

Does that mean both the instance variable and the local variable myBar are referring to the same object ont heap.

Also,
the local reference variable changed the value (from 28 to 99)
in 2,
But in the second case in 5 the local reference variable cannot change the value to 420.
What's the difference? Aren't they both Local reference variable.
So how can one modify the value while the other cannot.

Lastly,
IS there something called Static Instance Variable and Non Static instance variable.
THe part where I can't directly use f.num and have to use f.myBar.num.

Michael Swierczek
Ranch Hand

Joined: Oct 07, 2005
Posts: 107
    
    1
Nabila Mohammad,
If it helps, I think it's easy to see what happens if you just change the variable name of the shadowing variable. The result is the same.




Does that help?
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks Michael.
That helped Big Time!
Makes more sense.

Cheers!
 
 
subject: Shadowing of Variables