Java Reference
In-Depth Information
The scope and shadowing of a type or member imported by these declarations is specified
in § 6.3 and § 6.4 .
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
package cannot be imported. As such, § 7.5.1 , § 7.5.2 , § 7.5.3 , and § 7.5.4 all require a
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
Vector in the package java.util in all places where it is not shadowed (§ 6.4.1 ) or obscured
6.4.2 ) by a declaration of a field, parameter, local variable, or nested type declara-
tion with the same name.
Note that java.util.Vector is declared as a generic type (§ 8.1.2 ) . Once imported, the 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
Search WWH ::




Custom Search