• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Class variables always visible from within the class?

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm studying for SCJP 1.5. As you probably know, Marcus Green has a very handy SCJP question of the day online.

Today the answer states that the following is true:

"A static variable declared as public at class level (a field) will always be visible from code anywhere else in the class."



I'm not sure about this, as I had understood a static variable was not visible if it's shadowed:



According to the 3rd edition of the Java Language Specification:

"A declaration d is said to be visible at point p in a program if the scope of d includes p, and d is not shadowed by any other declaration at p."



Have I misunderstood something?

Thanks for any help,
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Local variables are always given more preference than global variables . This does'nt mean that the static variable is not visible inside the method . If you remove the local variable, you can get " Am i always visible? " as output.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static variable should be referenced like this (taking your Foo class as an exampe): Foo.someVariable

So it is is visible in the main method.
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies, but bear with me for a minute, I want to make sure I understand why the statement is true. I understand that we can reference the class variable from anywhere in the class using the qualified name "Foo.someVariable" notation, and that if we removed the local variable, it would then be accessible through the simple name "someVariable".

However, looking at the definition of visible given in the top post, there are two conditions which need to be satisfied, the second being that the declaration "is not shadowed by any other declaration" at that point. According to the same document:

Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.


Where

A simple name is a single identifier. A qualified name consists of a name, a "." token, and an identifier.


So is it the case that the local variable shadows the class variable at the point where System.out.println() is called? If so then surely, according to the earlier definition, the class variable is no longer visible at that point?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the JLS definitions, Matt is correct - a shadowed declaration is not visible (within the scope of the shadowing declaration). So technically Marcus Green's guide is incorrect here. In terms of the SCJP however, it is most likely irrelevant as the SCJP 1.5 is generally written in a way to avoid testing you on terminology, instad focusing on how the code would actually behave. In this context, it's sufficient to know that if one declaration shadows another, the shadowed declaration is inaccessible using the simple name, but accessible using the qualified name. Whether you call it "visible" or not is unimportant for the SCJP. However it is important when interpreting other sections of the JLS. When reading the JLS, stick rigidly to JLS definitions; when reading or discussing from other sources, allow for the possibility that they may be using slightly more... flexible... definitions.
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers, and it's reassuring to know that SCJP questions won't hinge on particular precise definitions of words.
 
reply
    Bookmark Topic Watch Topic
  • New Topic