Java Reference
In-Depth Information
Note, however, that it is not an error for the name of a class to also name a type that
otherwise might be imported by a type-import-on-demand declaration (§ 7.5.2 ) in the
compilation unit (§ 7.3 ) containing the class declaration. Thus, in this program:
Click here to view code image
package test;
import java.util.*;
class Vector {} // not a compile-time error
the declaration of the class Vector is permitted even though there is also a class
java.util.Vector . Within this compilation unit, the simple name Vector refers to the class
test.Vector , not to java.util.Vector (which can still be referred to by code within the com-
pilation unit, but only by its fully qualified name).
Example 7.6-2. Scope of Top Level Types
Click here to view code image
package points;
class Point {
int x, y; // coordinates
PointColor color; // color of this point
Point next; // next point with this color
static int nPoints;
}
class PointColor {
Point first; // first point with this color
PointColor(int color) { this.color = color; }
private int color; // color components
}
This program defines two classes that use each other in the declarations of their class
members. Because the class types Point and PointColor have all the type declarations in
package points , including all those in the current compilation unit, as their scope, this
program compiles correctly. That is, forward reference is not a problem.
The scope and shadowing of a top level type is specified in § 6.3 and § 6.4 .
The fully qualified name of a top level type is specified in § 6.7 .
Example 7.6-3. Fully Qualified Names
class Point { int x, y; }
Search WWH ::




Custom Search