Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shadowing

 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Could anyone tell me "How could I find that local variable is shadowing with the instance variable"

pls give some example

Sathya
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example.

 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From KB book


class Bar
{
int barnum = 28;
}
class Foo
{
Bar mybar = new Bar();
void changeIt(Bar myBar)
{
mybar.barnum = 99;
System.out.println("my bar.barnum in changeit is "+ mybar.barnum);
mybar = new bar();
mybar.barnum = 420;
System.out.println("mybar.barnum in changeit is now" + mybar.barnum);
}

public static void main(String [] args)
{
foo f = new foo();
System.out.println("f.mybar.barum is " + f.mybar.barnum);
f.changeit(f.mybar);
System.out.println("f.mybar.barnum after changeit is " + f.mybar.barnum);
}

}

O/p is

f.mybar.barnum is 28
mybar.barnum in change it is 99
mybar.barnum in change is now 420
f.mubar.barnum after changeit is 99


I don't understand how 99 is displaying at last. Pls give me some explanation



 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith

Thanks for your reply on time. Could you pls tell me how 99 is displaying in the o/p. I am reading java by myself , pls help me
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that in the first line of the main method, you
create a new Foo object, and its Bar instance variable is
initialized, and its barnum value is set to 28.

When you send that Foo object's Bar reference to changeIt, the
first thing that happens is that the Bar's barnum value is
changed to 99 and printed.

Now after it's printed, the formal parameter is set to
point to another object.

So from this point in the method, the original object which
the actual parameter refers to cannot be changed.

So after the method finishes executing, that Bar's
value of barnum is still 99.
 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now after it's printed, the formal parameter is set to
point to another object.

So from this point in the method, the original object which
the actual parameter refers to cannot be changed.

So after the method finishes executing, that Bar's
value of barnum is still 99.



But old object name is replaced by the same name with the new object. How come JVM knows its calling the original object or new object, but both have the same name and same variable name
 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kieth Could pls describe how original object is not replaced by the new object?

I really appreciate your effort and time
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
The output to the above code is 28,99,420,420.
i have run the program and checked but i ma not able to understand how
anyways refer to the below link which has similar code:
http://www.coderanch.com/t/263197/java-programmer-SCJP/certification/related-Shadowing
[ June 03, 2007: Message edited by: debasmita pattnayak ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi debasmita,

That link has the same code but a different discussion altogether.

Hi Sathya Shanmugam,



at line 1 :
the barnum member of the received argument (actual argument or parameter) is accessed and set with the value 99. Hope it is clear.

at line 2 :
At line 2, you are explicitly creating a *brand new* object through the new operator and assigning the object reference to the same reference variable "mybar" which was pointing to a originally received object till then.

After this statement, mybar is nowhere accessing the actually received object.

at line 3 :
You are assigning the value 420 to the barnum instance variable of the same reference variable "mybar". Since mybar is not pointing to the actually received object and a totally new object, the change is done only on the new object.

Since the new object is created within the changeIt() method and it not returned back as well, the object is nowhere accessible later beyond this method.

After the method execution is over, the control returns back to the place of calling - in main() method where you had called, f.changeIt(f.mybar);

Here the object which the reference variable 'f' was pointing still the same old object which was sent to changeIt(). Inside the changeIt() the state of object was changed in line 1 (ie, barnum set to value '99'). So you get the value '99' in the last SOP.

Hope this helps.

[ June 03, 2007: Message edited by: Raghavan Muthu ]
[ June 03, 2007: Message edited by: Raghavan Muthu ]
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavan,
its not clear. i think you are explaining to why we got 28,99,420,99.
But the actual answer to this is 28,99,420,420.
you can check this by running the program.
please clarify

Thanks
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi debasmita,

the reason is that your code contains typing errors (mybar vs myBar). If you correct those errors, you will get the expected result.
[ June 04, 2007: Message edited by: Manfred Klug ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi debasmita,

As Manfred pointed out,



The actual argument received was "myBar" but what you inturn change inside the method is with respect to the class/instance variable "mybar". See the difference.

In the main() method, after the call to changeIt() you invoke the barnum value with respect to the same class level reference "mybar" which was actually changed inside the method.

Change the line 2 to 4 inside changeIt() method as



and then run it. You will see the difference.

Hope this clears.
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavan,

your suggested change has one drawback. It doesn't demonstrate shadowing (the name of the parameter != the name of the instance variable).

For shadowing the method should look like this:

 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic