Java Reference
In-Depth Information
The scope and shadowing of a type or member imported by these declarations is specified
An
import
declaration makes types or members available by their simple names only
within the compilation unit that actually contains the
import
declaration. The scope of
the type(s) or member(s) introduced by an
import
declaration specifically does not in-
clude the
PackageName
of a
package
declaration, other
import
declarations in the cur-
rent compilation unit, or other compilation units in the same package.
A type in an unnamed package (§
7.4.2
) has no canonical name, so the requirement
for a canonical name in every kind of import declaration implies that (a) types in an
unnamed package cannot be imported, and (b)
static
members of types in an unnamed
compile-time error on any attempt to import a type (or
static
member thereof) in an
unnamed package.
7.5.1. Single-Type-Import Declarations
A
single-type-import declaration
imports a single type by giving its canonical name, mak-
ing it available under a simple name in the class and interface declarations of the compila-
tion unit in which the single-type-import declaration appears.
SingleTypeImportDeclaration:
import
TypeName
;
The
TypeName
must be the canonical name (§
6.7
) of a class type, interface type, enum
type, or annotation type.
It is a compile-time error if the named type is not accessible (§
6.6
).
Example 7.5.1-1. Single-Type-Import
import java.util.Vector;
causes the simple name
Vector
to be available within the class and interface declara-
tions in a compilation unit. Thus, the simple name
Vector
refers to the type declaration
tion with the same name.
Vector
can be used without qualification in a parameterized type such as
Vector<String>
,
or as the raw type
Vector
. This highlights a limitation of the
import
declaration: a type