Java Reference
In-Depth Information
• any static type named n imported by a static-import-on-demand declaration in c ;
• any top level type (§ 7.6 ) named n declared in another compilation unit (§ 7.3 ) of p ;
• any type named n imported by a type-import-on-demand declaration (§ 7.5.2 ) in c .
Example 6.4.1-1. Shadowing of a Field Declaration by a Local Variable Declara-
tion
Click here to view code image
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
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 shadowed 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 (§ 6.4 ) ; in
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 .
Search WWH ::




Custom Search