Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

Java package and import

Reference for the Java package and import statements: declare packages, import classes, use static imports, avoid name clashes and understand java.lang.

A package is a named group of related classes and interfaces. The package declaration at the top of a source file says which package the types in that file belong to. Packages organize code, prevent name clashes, and are the unit that package-private access is based on.

An import declaration lets you use a type by its simple name (List) instead of its fully qualified name (java.util.List). Imports are purely a compile-time convenience: they do not load anything and have no effect on performance or on the generated bytecode.

Java
package com.example.shop;      // must be the first statement (comments excepted)

import java.util.List;         // single-type import
import java.util.*;            // on-demand (wildcard) import
import static java.lang.Math.max;   // static import of one member
import static java.lang.Math.*;     // static import of all static members

Try it

Java Imports and fully qualified names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Static imports

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
PackageDeclaration:  package qualified.name;
ImportDeclaration:   import qualified.TypeName;
                     import qualified.package.*;
                     import static qualified.TypeName.memberName;
                     import static qualified.TypeName.*;
Form Meaning
package a.b.c; The types in this file belong to package a.b.c. At most one declaration per file, and it must come before any import or type.
import a.b.C; Single-type import: the simple name C refers to a.b.C.
import a.b.*; On-demand import: all public types of package a.b are available by simple name. It does not import sub-packages.
import static a.b.C.m; Makes the static member m (field, method, or nested type) of C usable without the C. prefix.
import static a.b.C.*; Makes all accessible static members of C usable by simple name.
import module java.base; Module import declaration (a preview feature in recent releases, see Usage notes).

Usage notes

  • File layout: by convention the package name mirrors the directory structure (com/example/shop/Cart.java declares package com.example.shop;), and names are lowercase, often starting with a reversed domain name. Without a package declaration, the type is in the unnamed package, which is fine for small experiments but cannot be imported from named packages.
  • java.lang is imported into every file automatically. That is why String, Math, Integer, and System need no import.
  • Wildcards are not recursive. import java.util.*; brings in List and Map but not java.util.function.Function or java.util.stream.Stream.
  • Wildcards do not cost anything at run time, but single-type imports document dependencies better and avoid surprises when a package gains a new class. Many style guides forbid wildcard imports; IDEs switch to a wildcard only after a threshold.
  • Name clashes: a single-type import shadows an on-demand import. If two single-type imports use the same simple name, the file does not compile; import one and use the fully qualified name of the other (as with java.util.Date and java.sql.Date above). Using an ambiguous name that came from two wildcard imports is also an error, but only where the name is actually used.
  • Static imports make code shorter but can hide where a name comes from. They suit constants and well-known helpers (Math.max, Collectors.toList, test assertions) and are a poor fit for application classes with generic names.
  • Scope: imports apply to the whole file (the compilation unit). They must appear after the package line and before the first type declaration.
  • Package-private access (no modifier) is limited to the same package. The same package name in a different module or class loader is a different runtime package. See public, protected, private.
  • Module imports: import module java.base; imports all packages exported by a module. It was introduced as a preview feature in Java 23 and its status can differ between releases, so check the release notes of your JDK before relying on it.
  • Sealed hierarchies without a module must keep permitted subclasses in the same package; see sealed.