Sub topic 1
Recap: Single-Type and On-Demand Imports
Prerequisites
This chapter assumes that you can write and compile basic Java classes. You should know what a package is and how to use fully qualified names such as java.util.List. You should also know that an import declaration lets source code use a shorter name.
Java imports are source-code conveniences. They connect a simple name, such as List, to a type, such as java.util.List. An import does not copy a class into your program, and it does not change the classpath or module path.
Single-type imports
A single-type import names one type:
import java.util.ArrayList;After this declaration, code in the compilation unit can use ArrayList instead of java.util.ArrayList.
A compilation unit is one Java source file together with its package declaration and import declarations. An import applies only to that compilation unit. It does not affect other source files in the same project.
Single-type imports are precise and easy to read:
import java.time.LocalDate;
import java.util.List;
public class Schedule {
public static void main(String[] args) {
List<LocalDate> dates = List.of(
LocalDate.of(2026, 1, 1),
LocalDate.of(2026, 12, 31));
System.out.println(dates);
}
}The compiler still checks whether the imported type is accessible. For example, a package-private type cannot be imported from a different package. A public type can be imported when the relevant package and module are available.
You can always avoid an import by writing the fully qualified name:
java.util.List<String> names = java.util.List.of("Ada", "Grace");This is useful when two types have the same simple name or when a name would otherwise be unclear.
On-demand imports
An on-demand import, commonly called a wildcard import, names a package followed by .*:
import java.util.*;This lets the source file use accessible types directly in java.util, such as List, Map, and HashMap.
The wildcard does not import subpackages. The following declaration makes types in java.util.concurrent available:
import java.util.concurrent.*;It is separate from import java.util.*;.
An on-demand import also does not import members such as static methods. Static imports use a different syntax:
import static java.util.Collections.emptyList;The compiler resolves a wildcard import when a simple name is used. Merely declaring import java.util.*; does not create an error if the package contains several types whose names are never used.
Comparing the two forms
| Form | Example | What it makes available |
|---|---|---|
| Single-type import | `import java.util.List;` | One named type |
| On-demand import | `import java.util.*;` | Accessible types directly in one package |
Single-type imports document exactly which types a file uses. On-demand imports reduce the number of import lines, but readers must inspect the package to know where a simple name comes from.
Neither form recursively searches packages. Neither form imports types that are not accessible. Both forms affect name lookup in the source file, not the behavior of compiled objects at runtime.
A first name clash
Suppose a file imports two packages that both contain a type named Date:
import java.util.*;
import java.sql.*;
public class DateExample {
public static void main(String[] args) {
// Date today = new Date(); // Compile-time error: Date is ambiguous
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(utilDate);
System.out.println(sqlDate);
}
}The compiler cannot guess which Date the programmer means. Use a fully qualified name at the point of use, or replace a wildcard import with a single-type import when one type is the intended choice.
How imports fit into name lookup
Java source can obtain a type name from several places, including the current package, explicit single-type imports, on-demand imports, and declarations in the source file itself. The compiler applies Java's name-resolution rules rather than choosing based on which import line appears first.
A useful practical rule is:
- Prefer a single-type import for an important or easily confused type.
- Use a fully qualified name when two required types share a simple name.
- Treat wildcard imports as a package-level convenience, not as a way to solve ambiguity.
The next chapters build on this model when they discuss module imports. A module import expands the idea from one package to the public types exported by a module, but the existing rules for simple names and conflicts still matter.
Exercise
What does import java.util.*; make available, and what does it leave out?
Show answer
It makes accessible types directly in java.util available by simple name. It does not import subpackages such as java.util.concurrent, and it does not import static members.