Packages and imports
What you will learn
A package gives related Java types a shared naming context. An import lets one source file use a shorter name for an accessible type or static member. It does not copy code into the file or change its visibility.
- Distinguish a simple name from a qualified type name.
- Organize and compile a two-package source fixture and resolve a naming collision.
Use a fresh lab folder for each example, JDK 21 or compatible newer JDK, no external libraries. Create the listed files under src; commands are run from that lab folder. The source compatibility target is Java 21; Java 25 execution is not yet verified.
Give a book a qualified name
package demo.catalog;public class Book { private final String title; public Book(String title) { this.title = title; } public String title() { return title; }}package demo.app;import demo.catalog.Book;public class Main { public static void main(String[] args) { Book book = new Book("River Maps"); System.out.println(book.title()); }}Compile and run:
javac --release 21 -d out src/demo/catalog/Book.java src/demo/app/Main.javajava -cp out demo.app.MainExpected output:
River MapsThe application imports the public Book type. Its qualified name is demo.catalog.Book. The constructor and title method must also be accessible.
Names, package declarations and imports
Place a package declaration before ordinary imports and type declarations. A source file without a package declaration is in an unnamed package; keep that convenience for tiny experiments, not reusable named-package libraries. A package name such as demo.catalog is a sequence of identifiers separated by dots. For these javac fixtures, save source files under a src root with paths matching their packages. A public top-level class Book belongs in Book.java. A normal type import applies to that compilation unit, not other files in the package. Types in the same package and public java.lang types normally need no import. An on-demand type import such as import java.util.*; does not recursively import subpackages. If two on-demand imports make a used simple type name ambiguous, qualify the intended type or import it explicitly. An explicit import of two different types with the same simple name is itself invalid. Static imports are for accessible static members, not arbitrary instance members. Java 25 also has module import declarations; the current Java 21 examples use ordinary type and static imports only.
Use two types with the same simple name
package demo.catalog;public class Book { private final String title; public Book(String title) { this.title = title; } public String title() { return title; }}package demo.archive;public class Book { public String shelf() { return "Archive A"; }}package demo.app;import demo.catalog.Book;public class Main { public static void main(String[] args) { Book active = new Book("River Maps"); demo.archive.Book archived = new demo.archive.Book(); System.out.println(active.title()); System.out.println(archived.shelf()); }}Compile and run:
javac --release 21 -d out src/demo/catalog/Book.java src/demo/archive/Book.java src/demo/app/Main.javajava -cp out demo.app.MainExpected output:
River MapsArchive AThe explicit import supplies Book for the active catalog. The archive type is named in full; importing both under the same simple name would fail.
Import a static method
package demo.app;import static java.lang.Math.max;public class Main { public static void main(String[] args) { System.out.println(max(3, 8)); System.out.println(Math.min(3, 8)); }}Compile and run:
javac --release 21 -d out src/demo/app/Main.javajava -cp out demo.app.MainExpected output:
83The static import makes max callable without Math. Math itself is available through the implicit java.lang import. The call still invokes the same Math method.
Common mistakes
Do not try to import a type from the unnamed package into a named package, and do not expect an import to bypass access checks. Prefer precise imports when multiple libraries reuse names.
Practice
Create demo.labels.Label with a public static text() method returning Shelf B. Create demo.app.Main with an explicit type import and print that value. Add a second label class under demo.archive returning Archive B, and print both by qualifying the archive type.
Hints:
- Create a separate file for each public class under src.
- Use demo.archive.Label.text() for the second class instead of importing two Label types.
Related reference topics: Java package declaration, Java single type import, Java qualified type names, Java static import. Next: Access modifiers.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Packages and imports — knowledge check
Apply package, visibility and classpath rules in Java 21-compatible fixtures.
What does an ordinary import change?
What is the qualified name of Book declared in demo.catalog?
Does import java.util.*; also import java.util.concurrent types?
Two accessible classes are named Book. Which is valid?
What does import static java.lang.Math.max; allow?