File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shadowing" Watch "Shadowing" New topic
Author

Shadowing

Sathya Shanmugam
Ranch Hand

Joined: Mar 02, 2007
Posts: 45
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

Joined: Feb 07, 2005
Posts: 2367
Here is an example.

Sathya Shanmugam
Ranch Hand

Joined: Mar 02, 2007
Posts: 45
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

Joined: Mar 02, 2007
Posts: 45
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Mar 02, 2007
Posts: 45
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

Joined: Mar 02, 2007
Posts: 45
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

Joined: Apr 12, 2007
Posts: 94
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 ]

debasmita
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Jun 04, 2007
Posts: 377
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:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shadowing
 
Similar Threads
Technical Interview Questions
Final keyword???
Shadow variables
constructor's call
Overriding Khalid example