Meaning of the import statement in a Java file

import declarations (not statements) are essentially short-hand enabler at the source code level: it allows you to refer to a type or a static member using a single identifier (e.g. List, min) as opposed to the fully qualified name (e.g. java.util.List, Math.min).

import declaration section is a compile-time element of the source codes, and has no presence at run-time. In JVM bytecodes, type names are always fully qualified, and unless you’re using a poorly written compiler, the binary should only contain names for types that are actually being used.

Class loaders are used for an entirely different concept, and has nothing to do with import feature at all.

JLS 7.5 Import Declarations

An import declaration allows a static member or a named type 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, or a static member of another type, is to use a fully qualified name.

A single-type-import declaration imports a single named type, by mentioning its canonical name.

A type-import-on-demand declaration imports all the accessible types of a named type or package as needed. It is a compile time error to import a type from the unnamed package.

A single static import declaration imports all accessible static members with a given name from a type, by giving its canonical name.

A static-import-on-demand declaration imports all accessible static members of a named type as needed.

References

See also


Various import related questions

On the grammatical role of import:

On on-demand vs single-type:

On import static:

Performance-related issues:

Leave a Comment