10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6.3.1 Shadowing Declarations NAMES<br />

120<br />

A single-static-import declaration d in a compilation unit c of package p that<br />

imports a field named n shadows the declaration of any static field named n<br />

imported by a static-import-on-demand declaration in c, throughout c.<br />

A single-static-import declaration d in a compilation unit c of package p that<br />

imports a method named n with signature s shadows the declaration of any static<br />

method named n with signature s imported by a static-import-on-demand declaration<br />

in c, throughout c.<br />

A single-static-import declaration d in a compilation unit c of package p that<br />

imports a type named n shadows the declarations of:<br />

• any static type named n imported by a static-import-on-demand declaration in<br />

c.<br />

• any top level type (§7.6) named n declared in another compilation unit (§7.3)<br />

of p.<br />

• any type named n imported by a type-import-on-demand declaration (§7.5.2)<br />

in c.<br />

throughout c.<br />

A type-import-on-demand declaration never causes any other declaration to<br />

be shadowed.<br />

A static-import-on-demand declaration never causes any other declaration to<br />

be shadowed.<br />

A declaration d is said to be visible at point p in a program if the scope of d<br />

includes p, and d is not shadowed by any other declaration at p. When the program<br />

point we are discussing is clear from context, we will often simply say that a<br />

declaration is visible.<br />

Note that shadowing is distinct from hiding (§8.3, §8.4.8.2, §8.5, §9.3, §9.5).<br />

Hiding, in the technical sense defined in this specification, applies only to members<br />

which would otherwise be inherited but are not because of a declaration in a<br />

subclass. Shadowing is also distinct from obscuring (§6.3.2).<br />

Here is an example of shadowing of a field declaration by a local variable declaration:<br />

class Test {<br />

static int x = 1;<br />

public static void main(String[] args) {<br />

int x = 0;<br />

System.out.print("x=" + x);<br />

System.out.println(", Test.x=" + Test.x);<br />

}<br />

}<br />

produces the output:<br />

DRAFT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!