Java Reference
In-Depth Information
• any static type named
n
imported by a static-import-on-demand declaration in
c
;
Example 6.4.1-1. Shadowing of a Field Declaration by a Local Variable Declara-
tion
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);
}
}
This program produces the output:
x=0, Test.x=1
This program 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
class variable
x
would normally be available throughout the entire body of the method
main
. In this example, however, the class variable
x
is shadowed within the body of the
method
main
by the declaration of the local variable
x
.
this case this is the rest of the body of the
main
method, namely its initializer “
0
” and
the invocations of
System.out.print
and
System.out.println
.
This means that:
• The expression
x
in the invocation of
print
refers to (denotes) the value of the
local variable
x
.