NAMES
Hiding Names
6.3.1
6.3.1 Hiding Names
Some declarations may be hidden ( 6.3.1) in part of their scope by another decla
ration of the same name, in which case a simple name cannot be used to refer to
the declared entity.
The example:
class Test {
static int x = 1;
public static void main(String[] args) {
int x = 0;
System.out.print("x=" + x);
System.out.println(", Test.x=" + Test.x);
}
}
produces the output:
x=0, Test.x=1
This example declares:
a class
Test
a class (
static
) variable
x
that is a member of the class
Test
a class method
main
that is a member of the class
Test
a parameter
args
of the
main
method
a local variable
x
of the
main
method
Since the scope of a class variable includes the entire body of the class ( 8.2)
the class variable
x
would normally be available throughout the entire body of the
method
main
. In this example, however, the class variable
x
is hidden within the
body of the method
main
by the declaration of the local variable
x
.
A local variable has as its scope the rest of the block in which it is declared
( 14.3.2); in this case this is the rest of the body of the
main
method, namely its
initializer
0
and the invocations of
print
and
println
.
This means that:
The expression
x
in the invocation of
print
refers to (denotes) the value of
the local variable
x
.
The invocation of
println
uses a qualified name ( 6.6)
Test.x
, which uses
the class type name
Test
to access the class variable
x
, because the declara
tion of
Test.x
is hidden at this point and cannot be referred to by its simple
name.
83
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved