Syntax
>>-import-+-TypeName--------+-;->< '-PackageName-.-*-'
Description
An import declaration allows a type declared in another package to
be referred to by a simple name that consists of a single identifier. Without the use of
an appropriate import declaration, the only way to refer to a type declared in another
package is to use its fully qualified name.
An import declaration makes types available by their simple names only within the compilation unit that actually contains the import declaration. The scope of the name that it introduces specifically does not include the package statement, other import statements in the current compilation unit, or other compilation units in the same package.
A single-type-import declaration imports a single type by indicating its fully
qualified name, making it available under a simple name in the class and interface
declarations of its compilation unit. If the named type does not exist, a compilation
error occurs, so the TypeName must be the fully qualified name of a class or
interface type. If the named type is not in the current package, then it must be
accessible, that is, in an accessible package and declared public; otherwise
a compilation error occurs.
If two single-type-import declarations in the same compilation unit attempt to import
types with the same simple name, then a compilation 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, then a compilation error occurs.
A type-import-on-demand declaration allows all public types declared in the
package named by a fully qualified name to be imported as needed. A type-import-on-demand
declaration cannot name a package that is not accessible, as determined by the host
system, or a compilation error occurs. Two or more type-import-on-demand declarations in
the same compilation unit may name the same package; the effect is as if there were
exactly one such declaration. It is not a compilation error to name the current package or
java.lang in a type-import-on-demand declaration, even though they are already imported;
the duplicate type-import-on-demand declaration is ignored.
Each compilation unit automatically imports each of the public type names declared in the
predefined package java.lang, as if the following declaration appeared at the beginning of
each compilation unit, immediately following any package statement:
import java.lang.*;
Note that an import statement cannot import a subpackage, only a type.
Examples
The following example makes the simple name Vector available within the class and
interface declarations in a compilation unit:
import java.util.Vector;
The following example makes the simple names of all public types declared in the
package java.util available within the class and interface declarations of the compilation
unit:
import java.util.*;
Thus, the simple name Vector refers to the type Vector in the package java.util in all
places where it is not hidden by a single-type-import declaration of a type whose simple
name is Vector; by a type named Vector and declared in the package to which the
compilation unit belongs; or by a declaration of a field, parameter, or local variable
named Vector.
Syntax diagrams
Java types
class keyword
interface keyword
Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.