File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Shadowing Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shadowing Variables" Watch "Shadowing Variables" New topic
Author

Shadowing Variables

Laloo Bond
Greenhorn

Joined: Jun 06, 2008
Posts: 5


The output of this program is :
-------------------------------
Before modify size variable 7
size inside changeIt 207
size After changeIt 7


Am little bit confused about the behaviour of this program's output.
As per my knowledge,here SIZE is a static variable and it should be unique.
I believe that is main purpose of static variables and it is accessed globally without creating instance of it.

But here in the changeIt(int size) method, as soon as we create the local(method) variable as the same name of static variable, the global variable is shadowed(hidden) by the local variable.

I thought the output is like :
Before modify size variable7
size inside changeIt207
size After changeIt 207

because size is global variable and unique as it is static.

Could you please explain how it is working and what is the priority and scope of both method(local) variable and static(global) variable.

[edit]Add code tags. CR[/edit]

[ June 20, 2008: Message edited by: Campbell Ritchie ]
[ June 20, 2008: Message edited by: Laloo Bond ]
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Remember that a variable passed as argument is always a copy. In the function, the size is a copy and shadows the static size. You can do anything with it, but the original size variable is not affected.

Try to use the code tags



Java hobbyist.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Welcome to JavaRanch, Laloo but please don't post the same question twice. I have closed your duplicate thread.
As Ronald Schild has already told you, make sure to use the CODE button (below the "message" window) for quoted code; I have amended your post so you can see how much easier it is to read.

You have actually got two variables called size inside your changeIt method, one which is a (static) field and one which is a parameter. The parameter (as you have been told) shadows the field (a local variable would behave similarly), so the JVM "sees" the parameter only; to it size means the parameter. If you want it to mean the field you would have to write Foo.size (for an instance field write this.size).
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Originally posted by Campbell Ritchie:
Welcome to JavaRanch, Laloo but please don't post the same question twice. I have closed your duplicate thread.
As Ronald Schild has already told you, make sure to use the CODE button (below the "message" window) for quoted code; I have amended your post so you can see how much easier it is to read.


Please make sure you include the package statement in the code tags.

(couldn't resist, sorry it's friday and I'm almost heading home )
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Originally posted by Ronald Schild:


Please make sure you include the package statement in the code tags.

(couldn't resist, sorry it's friday and I'm almost heading home )


jeez, what time zone are you in?
Happy Friday!!


SCJP 5.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Originally posted by Keith Nagle:
jeez, what time zone are you in?
Obviously one ahead of yours

Laloo Bond
Greenhorn

Joined: Jun 06, 2008
Posts: 5
Thanks for everyone , who replied and guided me how to post a message with proper readability for the code part in the message.

am in APAC timezone

Now its time for me to think about and enjoy the weekend..

Happy Weekend ...

cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shadowing Variables