• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shadowing Variables

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 )
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Nagle:
jeez, what time zone are you in?

Obviously one ahead of yours

 
Laloo Bond
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic