A single-type-import declaration imports a single type by giving its fully qualified name, making it available under a simple name in the class and interface declarations of its compilation unit.
SingleTypeImportDeclaration:
import
TypeName;
The TypeName must be the fully qualified name of a class or interface type; a
compile-time error occurs if the named type does not exist. If the named type is
not in the current package, then it must be accessible (§6.6)-in an accessible
package and declared public
(§8.1.2, §9.1.2)-or a compile-time error occurs.
import java.util.Vector;
causes the simple name Vector
to be available within the class and interface declarations in a compilation unit. Thus, the simple name Vector
refers to the type
Vector
in the package java.util
in all places where it is not hidden (§6.3) by a
declaration of a field, parameter, or local variable with the same name.
If two single-type-import declarations in the same compilation unit attempt to import types with the same simple name, then a compile-time error occurs, unless the two types are the same type, in which case the duplicate declaration is ignored. If another type with the same name is otherwise declared in the current compilation unit except by a type-import-on-demand declaration (§7.5.2), then a compile-time error occurs.
import java.util.Vector;
class Vector { Object[] vec; }
causes a compile-time error because of the duplicate declaration of Vector
, as
does:
import java.util.Vector;
import myVector.Vector;
where myVector
is a package containing the compilation unit:
package myVector;
public class Vector { Object[] vec; }
The compiler keeps track of types by their fully qualified names (§6.7). Simple names and fully qualified names may be used interchangeably whenever they are both available.
Note that an import statement cannot import a subpackage, only a type. For example, it does not work to try to import java.util
and then use the name util.Random
to refer to the type java.util.Random
:
import java.util; // incorrect: compile-time error
class Test { util.Random generator; }