---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 7. Packages"
section: "7.5"
heading: "7.5. Import Declarations"
parent: "Chapter 7. Packages"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.5
id: "7.5"
url: https://jvm.obfuscated.software/jls/se8/7.5.html
raw: https://jvm.obfuscated.software/jls/se8/7.5.md
---

# 7.5. Import Declarations

An *import declaration* allows a named type or a `static` member to be referred to by a simple name ([§6.2](6.2.md)) 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, or a `static` member of another type, is to use a fully qualified name ([§6.7](6.7.md)).

```
ImportDeclaration:
  SingleTypeImportDeclaration
  TypeImportOnDemandDeclaration
  SingleStaticImportDeclaration
  StaticImportOnDemandDeclaration
```

- A single-type-import declaration ([§7.5.1](7.5.1.md)) imports a single named type, by mentioning its canonical name ([§6.7](6.7.md)).
- A type-import-on-demand declaration ([§7.5.2](7.5.2.md)) imports all the accessible types ([§6.6](6.6.md)) of a named type or named package as needed, by mentioning the canonical name of a type or package.
- A single-static-import declaration ([§7.5.3](7.5.3.md)) imports all accessible `static` members with a given name from a type, by giving its canonical name.
- A static-import-on-demand declaration ([§7.5.4](7.5.4.md)) imports all accessible `static` members of a named type as needed, by mentioning the canonical name of a type.

The scope and shadowing of a type or member imported by these declarations is specified in [§6.3](6.3.md) and [§6.4](6.4.md).

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 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 type by giving its canonical name, making it available under a simple name in the 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 type, interface type, enum type, or annotation type ([§6.7](6.7.md)).

The name must be qualified ([§6.5.5.2](6.5.5.2.md)), or a compile-time error occurs.

It is a compile-time error if the named type is not accessible ([§6.6](6.6.md)).

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 the type imported by the single-type-import declaration is declared in the compilation unit that contains the `import` declaration, the `import` declaration is ignored.

If a single-type-import declaration imports a type whose simple name is n, and the compilation unit also declares a top level type ([§7.6](7.6.md)) whose simple name is n, a compile-time error occurs.

If a compilation unit contains both a single-type-import declaration that imports a type whose simple name is n, and a single-static-import declaration ([§7.5.3](7.5.3.md)) that imports a type whose simple name is n, a compile-time error occurs.

**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 type declaration `Vector` in the package `java.util` in all places where it is not shadowed ([§6.4.1](6.4.1.md)) or obscured ([§6.4.2](6.4.2.md)) by a declaration of a field, parameter, local variable, or nested type declaration with the same name.

Note that the actual declaration of `java.util.Vector` is generic ([§8.1.2](8.1.2.md)). 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 nested type declared inside a generic type declaration can be imported, but its outer type is always erased.

**Example 7.5.1-2. Duplicate Type 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` 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;
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](6.1.md). 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 types of a named package or type to be imported as needed.

```
TypeImportOnDemandDeclaration:
  import PackageOrTypeName . * ;
```

The *PackageOrTypeName* must be the canonical name ([§6.7](6.7.md)) of a package, a class type, an interface type, an enum type, or an annotation type.

If the *PackageOrTypeName* denotes a type ([§6.5.4](6.5.4.md)), then the name must be qualified ([§6.5.5.2](6.5.5.2.md)), or a compile-time error occurs.

It is a compile-time error if the named package or type is not accessible ([§6.6](6.6.md)).

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 type or package. 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](7.5.4.md)) that name the same type, the effect is as if the `static` member types of that type ([§8.5](8.5.md), [§9.5](9.5.md)) were imported only once.

**Example 7.5.2-1. Type-Import-on-Demand**

```
import java.util.*;
```

causes the simple names of all `public` types 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 type `Vector` in the package `java.util` in all places in the compilation unit where that type declaration is not shadowed ([§6.4.1](6.4.1.md)) or obscured ([§6.4.2](6.4.2.md)).

The declaration might be shadowed 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 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 type. This makes these `static` members available under their simple name in the 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](6.7.md)) of a class type, interface type, enum type, or annotation type.

The name must be qualified ([§6.5.5.2](6.5.5.2.md)), or a compile-time error occurs.

It is a compile-time error if the named type is not accessible ([§6.6](6.6.md)).

The *Identifier* must name at least one `static` member of the named type. 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 or types with the same name, or several methods with the same name and signature.

If a single-static-import declaration imports a type whose simple name is n, and the compilation unit also declares a top level type ([§7.6](7.6.md)) whose simple name is n, a compile-time error occurs.

If a compilation unit contains both a single-static-import declaration that imports a type whose simple name is n, and a single-type-import declaration ([§7.5.1](7.5.1.md)) that imports a type whose simple name is n, a compile-time error occurs.

## 7.5.4. Static-Import-on-Demand Declarations

A *static-import-on-demand declaration* allows all accessible `static` members of a named type to be imported as needed.

```
StaticImportOnDemandDeclaration:
  import static TypeName . * ;
```

The *TypeName* must be the canonical name ([§6.7](6.7.md)) of a class type, interface type, enum type, or annotation type.

The name must be qualified ([§6.5.5.2](6.5.5.2.md)), or a compile-time error occurs.

It is a compile-time error if the named type is not accessible ([§6.6](6.6.md)).

Two or more static-import-on-demand declarations in the same compilation unit may name the same type; 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 or types with the same name, or several methods with the same name and signature.

If a compilation unit contains both a static-import-on-demand declaration and a type-import-on-demand declaration ([§7.5.2](7.5.2.md)) that name the same type, the effect is as if the `static` member types of that type ([§8.5](8.5.md), [§9.5](9.5.md)) were imported only once.
