An import declaration allows a named class, interface, or static member to be referred to by a simple name (§6.2) that consists of a single identifier.
Without the use of an appropriate import declaration, a reference to a class or interface declared in another package, or a reference to a static member of another class or interface, would typically need to use a fully qualified name (§6.7).
ImportDeclaration:
SingleTypeImportDeclaration
TypeImportOnDemandDeclaration
SingleStaticImportDeclaration
StaticImportOnDemandDeclaration
SingleModuleImportDeclaration
- A single-type-import declaration (§7.5.1) imports a single named class or interface, by mentioning its canonical name (§6.7).
- A type-import-on-demand declaration (§7.5.2) imports all the accessible classes and interfaces of a named package, class, or interface as needed, by mentioning the canonical name of the package, class, or interface.
- A single-static-import declaration (§7.5.3) imports all accessible
staticmembers with a given name from a class or interface, by giving its canonical name. - A static-import-on-demand declaration (§7.5.4) imports all accessible
staticmembers of a named class or interface as needed, by mentioning the canonical name of the class or interface. - A single-module-import declaration (§7.5.5) imports all the accessible classes and interfaces of the packages exported by a named module, as needed.
The scope and shadowing of a class, interface, or member imported by these declarations is specified in §6.3 and §6.4.
An import declaration makes classes, interfaces, or members available by their simple names only within the compilation unit that actually contains the import declaration. The scope of the class(es), interface(s), or member(s) introduced by an import declaration specifically does not include other compilation units in the same package, other import declarations in the current compilation unit, or a package declaration in the current compilation unit (except for the annotations of a package declaration).
7.5.1. Single-Type-Import Declarations
A single-type-import declaration imports a single class or interface by giving its canonical name, making it available under a simple name in the module, class, and interface declarations of the compilation unit in which the single-type-import declaration appears.
SingleTypeImportDeclaration:
import TypeName ;
The TypeName must be the canonical name of a class or interface (§6.7).
The class or interface must be either a member of a named package, or a member of a class or interface whose outermost lexically enclosing class or interface declaration (§8.1.3) is a member of a named package, or a compile-time error occurs.
It is a compile-time error if the named class or interface is not accessible (§6.6).
If two single-type-import declarations in the same compilation unit attempt to import classes or interfaces with the same simple name, then a compile-time error occurs, unless the two classes or interface are the same, in which case the duplicate declaration is ignored.
If the class or interface imported by the single-type-import declaration is declared as a top level class or interface (§7.6) in the compilation unit that contains the import declaration, then the import declaration is ignored.
If a single-type-import declaration imports a class or interface whose simple name is x, and the compilation unit also declares a top level class or interface whose simple name is x, a compile-time error occurs.
If a compilation unit contains both a single-type-import declaration that imports a class or interface whose simple name is x, and a single-static-import declaration (§7.5.3) that imports a class or interface whose simple name is x, a compile-time error occurs, unless the two classes or interfaces are the same, in which case the duplicate declaration is ignored.
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 declarations in a compilation unit. Thus, the simple name Vector refers to the class 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 class or interface declaration with the same name.
Note that the actual declaration of java.util.Vector is generic (§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. A related limitation of the import declaration is that a member class or interface declared inside a generic class or interface declaration can be imported, but its outer type is always erased.
Example 7.5.1-2. Duplicate Class Declarations
This program:
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; }
Example 7.5.1-3. No Import of a Subpackage
Note that an import declaration cannot import a subpackage, only a class or interface.
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;
class Test { util.Random generator; }
// incorrect: compile-time error
Example 7.5.1-4. Importing a Type Name that is also a Package Name
Package names and type names are usually different under the naming conventions described in §6.1. Nevertheless, in a contrived example where there is an unconventionally named package Vector, which declares a public class whose name is Mosquito:
package Vector;
public class Mosquito { int capacity; }
and then the compilation unit:
package strange;
import java.util.Vector;
import Vector.Mosquito;
class Test {
public static void main(String[] args) {
System.out.println(new Vector().getClass());
System.out.println(new Mosquito().getClass());
}
}
the single-type-import declaration importing class Vector from package java.util does not prevent the package name Vector from appearing and being correctly recognized in subsequent import declarations. The example compiles and produces the output:
class java.util.Vector
class Vector.Mosquito
7.5.2. Type-Import-on-Demand Declarations
A type-import-on-demand declaration allows all accessible classes and interfaces of a named package, class, or interface to be imported as needed.
TypeImportOnDemandDeclaration:
import PackageOrTypeName . * ;
The PackageOrTypeName must be the canonical name (§6.7) of a package, a class, or an interface.
If the PackageOrTypeName denotes a class or interface (§6.5.4), then the class or interface must be either a member of a named package, or a member of a class or interface whose outermost lexically enclosing class or interface declaration (§8.1.3) is a member of a named package, or a compile-time error occurs.
It is a compile-time error if the named package is not uniquely visible to the current module (§7.4.3), or if the named class or interface is not accessible (§6.6).
It is not a compile-time error to name either java.lang or the named package of the current compilation unit in a type-import-on-demand declaration. The type-import-on-demand declaration is ignored in such cases.
Two or more type-import-on-demand declarations in the same compilation unit may name the same package, class, or interface. All but one of these declarations are considered redundant; the effect is as if that type was imported only once.
If a compilation unit contains both a type-import-on-demand declaration and a static-import-on-demand declaration (§7.5.4) that name the same class or interface, the effect is as if the static member classes and interfaces of that class or interface (§8.5, §9.5) are imported only once.
Example 7.5.2-1. Type-Import-on-Demand
import java.util.*;
causes the simple names of all public classes and interfaces declared in the package java.util to be available within the class and interface declarations of the compilation unit. Thus, the simple name Vector refers to the class Vector of the package java.util in all places in the compilation unit where that class declaration is not shadowed (§6.4.1) or obscured (§6.4.2).
The declaration might be shadowed by a single-type-import declaration of a class or interface whose simple name is Vector; by a class or interface named Vector and declared in the package to which the compilation unit belongs; or any nested classes or interfaces.
The declaration might be obscured by a declaration of a field, parameter, or local variable named Vector.
(It would be unusual for any of these conditions to occur.)
7.5.3. Single-Static-Import Declarations
A single-static-import declaration imports all accessible static members with a given simple name from a class or interface. This makes these static members available under their simple name in the module, class, and interface declarations of the compilation unit in which the single-static-import declaration appears.
SingleStaticImportDeclaration:
import static TypeName . Identifier ;
The TypeName must be the canonical name (§6.7) of a class or interface.
The class or interface must be either a member of a named package, or a member of a class or interface whose outermost lexically enclosing class or interface declaration (§8.1.3) is a member of a named package, or a compile-time error occurs.
It is a compile-time error if the named class or interface is not accessible (§6.6).
The Identifier must name at least one static member of the named class or interface. It is a compile-time error if there is no static member of that name, or if all of the named members are not accessible.
It is permissible for one single-static-import declaration to import several fields, classes, or interfaces with the same name, or several methods with the same name and signature. This occurs when the named class or interface inherits multiple fields, member classes, member interfaces, or methods, all with the same name, from its own supertypes.
If two single-static-import declarations in the same compilation unit attempt to import classes or interface with the same simple name, then a compile-time error occurs, unless the two classes or interfaces are the same, in which case the duplicate declaration is ignored.
If a single-static-import declaration imports a class or interface whose simple name is x, and the compilation unit also declares a top level class or interface (§7.6) whose simple name is x, a compile-time error occurs.
If a compilation unit contains both a single-static-import declaration that imports a class or interface whose simple name is x, and a single-type-import declaration (§7.5.1) that imports a class or interface whose simple name is x, a compile-time error occurs, unless the two classes or interfaces are the same, in which case the duplicate declaration is ignored.
7.5.4. Static-Import-on-Demand Declarations
A static-import-on-demand declaration allows all accessible static members of a named class or interface to be imported as needed.
StaticImportOnDemandDeclaration:
import static TypeName . * ;
The TypeName must be the canonical name (§6.7) of a class or interface.
The class or interface must be either a member of a named package, or a member of a class or interface whose outermost lexically enclosing class or interface declaration (§8.1.3) is a member of a named package, or a compile-time error occurs.
It is a compile-time error if the named class or interface is not accessible (§6.6).
Two or more static-import-on-demand declarations in the same compilation unit may name the same class or interface; the effect is as if there was exactly one such declaration.
Two or more static-import-on-demand declarations in the same compilation unit may name the same member; the effect is as if the member was imported exactly once.
It is permissible for one static-import-on-demand declaration to import several fields, classes, or interfaces with the same name, or several methods with the same name and signature. This occurs when the named class or interface inherits multiple fields, member classes, member interfaces, or methods, all with the same name, from its own supertypes.
If a compilation unit contains both a static-import-on-demand declaration and a type-import-on-demand declaration (§7.5.2) that name the same class or interface, the effect is as if the static member classes and interfaces of that class or interface (§8.5, §9.5) are imported only once.
7.5.5. Single-Module-Import Declarations
A single-module-import declaration allows all public top level classes and interfaces of the packages exported by a named module to be imported as needed.
SingleModuleImportDeclaration:
import module ModuleName ;
A single-module-import declaration imports, on demand, all the public top level classes and interfaces in the following packages:
- The packages exported by the module ModuleName to the current module.
- The packages exported by the modules that are read by the current module due to reading the module ModuleName. This allows a program to use the API of a module, which might refer to classes and interfaces from other modules, without having to import all those other modules.
It is a compile-time error if the module ModuleName is not read by the current module (§7.3).
The modules read by the current module are given by the result of resolution, as described in the java.lang.module package specification (§7.3).
Two or more single-module-import declarations in the same compilation unit may name the same module. All but one of these declarations are considered redundant; the effect is as if that module was imported only once.
A single-module-import declaration can be used in any source file. It is not required for the source file to be part of a module. For example, modules java.base and java.sql are part of the standard Java runtime, so they can be imported by programs which are not themselves developed as modules.
It is sometimes useful to import a module that does not export any packages. This is because the module may transitively require other modules that do export packages. For example, the java.se module does not export any packages, but it requires a number of modules transitively, so the effect of the single-module-import declaration import module java.se; is to import the packages which are exported by those modules (and so on, recursively).
Example 7.5.5-1. Single-Module-Import in Ordinary Compilation Units
Modules allow a set of packages to be grouped together for reuse under a single name, and the exported packages of a module are intended to form a cohesive and coherent API. Single-module-import declarations allow the developer to import all the packages exported by a module in one go, simplifying the reuse of modular libraries. For example:
import module java.xml;
causes the simple names of the public top level classes and interfaces declared in all packages exported by module java.xml to be available within the class and interface declarations of the compilation unit. Thus, the simple name XPath refers to the interface XPath of the package javax.xml.xpath exported by the module java.xml in all places in the compilation unit where that class declaration is not shadowed or obscured.
Assume the following compilation unit associated with module M0:
package q;
import module M1; // What does this import?
class C { ... }
where module M0 has the following declaration:
module M0 { requires M1; }
The meaning of the single-module-import declaration import module M1; depends on the exports of M1 and any modules that M1 requires transitively. Consider as an example:
module M1 {
exports p1;
exports p2 to M0;
exports p3 to M3;
requires transitive M4;
requires M5;
}
module M3 { ... }
module M4 { exports p10; }
module M5 { exports p11; }
The effect of the single-module-import declaration import module M1; is then:
- Import the
publictop level classes and interfaces from packagep1, sinceM1exportsp1to everyone; - Import the
publictop level classes and interfaces from packagep2, sinceM1exportsp2toM0, the module with which the compilation unit is associated; and - Import the
publictop level classes and interfaces from packagep10, sinceM1requires transitivelyM4, which exportsp10.
Nothing from packages p3 or p11 is imported by the compilation unit.
Single-module-import declarations may appear in a source file containing only a package declaration. Such files are typically called package-info.java and are used as the sole repository for package-level annotations and documentation (§7.4.1).
Example 7.5.5-2. Single-Module-Import in Modular Compilation Units
Import declarations can also appear in a modular compilation unit. The following modular compilation unit uses a single-module-import declaration, allowing the simple name of the interface Driver associated with module java.sql to be used in the provides directive:
import module java.sql;
module com.myDB.core {
exports ...
requires transitive java.sql;
provides Driver with com.myDB.greatDriver;
}
It is possible for a modular compilation unit that declares a module to also import that module. In the following example, this means that the simple name of a class C can be used in a uses directive:
import module M;
module M {
...
exports p;
...
uses C;
...
}
where the package p exported by module M is declared as follows:
package p;
class C { ... }
Without the single-module-import declaration, the qualified name of the class C would need to be used in the uses directive.
Suppose a module declaration as follows:
module M2 {
requires java.se;
exports p2;
...
}
where the package p2 exported by M2 is declared as follows:
package p2;
import module java.xml;
class MyClass {
...
}
Even though the module M2 does not directly express a dependency on the module java.xml, the import of module java.xml is still correct as the resolution process will determine that the module java.xml is read by module M2.
Example 7.5.5-3. Ambiguous Imports
Clearly importing multiple modules could lead to name ambiguities, for example:
import module java.base;
import module java.desktop;
...
List l = ... // Error - Ambiguous name!
...
The module java.base exports the package java.util, which has a public List interface. The module java.desktop exports the package java.awt, which a public List class. Having imported both modules, the use of the simple name List is clearly ambiguous and results in a compile-time error.
However, just importing a single module can also lead to a name ambiguity, for example:
import module java.desktop;
...
Element e = ... // Error - Ambiguous name!
...
The module java.desktop exports packages javax.swing.text and javax.swing.text.html.parser, which have a public Element interface and a public Element class, respectively. Thus the use of the simple name Element is ambiguous and results in a compile-time error.
A single-type-import declaration can be used to resolve a name ambiguity. The earlier example where the simple name List is ambiguous can be resolved as follows:
import module java.base;
import module java.desktop;
import java.util.List; // Resolving the ambiguity
// of the simple name List
...
List l = ... // Ok - List is resolved to
// java.util.List
...