Sub topic 7
What Is Imported Automatically?
Assume you already know Java variables, methods, classes, and explicit import declarations. This chapter focuses on compact source files in Java 25, where the compiler makes several useful packages available without import statements.
The rule for compact source files
A compact source file has top-level fields and methods instead of an explicit class declaration. For example:
void main() {
List<String> names = List.of("Ada", "Grace");
IO.println(names);
}This code uses List without writing import java.util.List;. A compact source file implicitly imports the packages exported by the java.base module. An implicit import is an import supplied by the language rules rather than written in the file.
The effect is similar to adding import-on-demand declarations such as import java.util.*;. It makes public types in those packages available by their simple names.
Common automatically available packages include:
| Package | Example types |
|---|---|
| `java.lang` | `String`, `Math`, `IO` |
| `java.util` | `List`, `Map`, `ArrayList` |
| `java.time` | `LocalDate`, `Instant` |
| `java.math` | `BigDecimal`, `BigInteger` |
| `java.util.regex` | `Pattern`, `Matcher` |
| `java.util.stream` | `Stream` |
The automatic imports cover the packages exported by java.base, not every package in the Java platform.
A working example
The following compact source file uses several types without import declarations:
void main() {
List<String> names = List.of("Ada", "Grace");
Map<String, Integer> scores = Map.of(
"Ada", 10,
"Grace", 12
);
LocalDate date = LocalDate.of(2026, 11, 26);
BigDecimal price = new BigDecimal("19.99");
IO.println("Date: " + date);
IO.println("Names: " + names);
IO.println("Grace's score: " + scores.get("Grace"));
IO.println("Price: $" + price);
}List and Map come from java.util. LocalDate comes from java.time. BigDecimal comes from java.math. IO comes from java.lang.
These names are available because all of those packages belong to java.base. The automatic import affects how the compiler resolves names. It does not download a library or add a module to the program. The types must still exist in the Java runtime or compilation environment.
What is not imported
Types in other platform modules are not automatically available. For example, JDBC types such as java.sql.Connection are outside java.base. A compact source file must import them explicitly:
import java.sql.Connection;
void main() {
IO.println(Connection.class.getName());
}The required module must also be available to the compiler and launcher. An import only tells Java which type name you intend to use.
You can always use a fully qualified name instead of an import:
void main() {
java.sql.Connection connection = null;
IO.println(connection);
}A fully qualified name includes the complete package and type name.
Common errors
Automatic imports do not make every type available. java.sql.Date is not available just because java.util.Date belongs to an automatically imported package.
Names can also become confusing when types with the same simple name are visible. Use the fully qualified name when clarity matters. You may also add an explicit single-type import. Keep imports explicit for less common dependencies so readers can see where a type comes from.
Exercise
Why can a compact source file use List and LocalDate without import statements, but not java.sql.Connection?
Show answer
List and LocalDate are in packages exported by java.base, which compact source files import automatically. java.sql.Connection is outside java.base, so it needs an explicit import or a fully qualified name.