Sub topic 10
Compact Source Files and the Implicit `java.base` Import
Assume you already know how to write and compile ordinary Java classes, use package names, and read basic module-info.java declarations. This chapter focuses on compact source files and how their automatic imports differ from imports that you write yourself.
What a compact source file is
A compact source file is a Java source file that omits the surrounding class declaration. It can contain imports, a main method, and statements directly at the top level. Java implicitly creates a class around that code.
For example, this file can run without an explicit class declaration:
void main() {
List<String> names = List.of("Ada", "Grace", "Alan");
names.stream()
.sorted()
.forEach(System.out::println);
}Save it as CompactDemo.java. The compiler treats the file as defining an implicitly declared class. The exact generated class name is based on the source file name.
A compact source file is useful for small programs, examples, and learning exercises. It removes ceremony, but it does not change the Java type system or the rules for access control.
The automatic `java.base` import
Every Java runtime includes the java.base module. It contains fundamental packages such as:
java.langjava.utiljava.iojava.niojava.netjava.util.stream
A compact source file implicitly imports every public top-level class and interface exported by java.base. Therefore, the previous example can use List without writing import java.util.List;.
This automatic import is similar in effect to:
import module java.base;The similarity is conceptual. You do not need to write that declaration in a compact source file.
The automatic import includes types, not every name associated with those types. For example, List is available, but its static methods are still called through the type name:
List<String> names = List.of("Ada", "Grace");You cannot call of("Ada", "Grace") directly unless you add a suitable static import.
What is and is not included
The implicit import includes only public top-level types that java.base exports. It does not make every class in the JDK available.
| Available without an import | Still requires an import |
|---|---|
| `List`, from `java.util` | `Date` from `java.sql` |
| `HashMap`, from `java.util` | `ArrayList` is available through `java.base` |
| `Collectors`, from `java.util.stream` | A type from a third-party module |
| `Path`, from `java.nio.file` | A non-exported internal JDK type |
java.sql is a separate module. Its types are not covered by the implicit java.base import:
import java.sql.Connection;
void main() {
Connection connection = null;
System.out.println(connection);
}The package java.sql is not part of java.base, so the explicit import is necessary.
Interaction with module imports
A compact source file can also contain ordinary imports and module import declarations. Adding another module can introduce an ambiguous simple name.
For example, both java.base and java.sql provide a public type named Date:
import module java.sql;
void main() {
// Date value = null; // ambiguous: java.util.Date and java.sql.Date
java.util.Date value = new java.util.Date();
System.out.println(value.getTime() > 0);
}The fully qualified name java.util.Date resolves the ambiguity. An explicit single-type import, such as import java.util.Date;, is another clear solution when that type is used repeatedly.
Version and compilation rules
Compact source files were introduced as a preview feature and became final in JDK 25. With JDK 25 or later, compile and run the example without preview flags:
javac --release 25 CompactDemo.java
java CompactDemoWhen using a preview release, use both --enable-preview and the matching --release value:
javac --release 23 --enable-preview CompactDemo.java
java --enable-preview CompactDemoThe compiler and launcher must use compatible Java versions. Do not compile with one preview release and run with another.
When to use this feature
Use a compact source file when the surrounding class adds little value, such as in a small command-line experiment or teaching example. Use an ordinary class when the code is part of a library, has several methods or fields, needs explicit constructors, or benefits from a visible class name and API boundary.
Exercise
Why does a compact source file use List without import java.util.List;, but still need an import for java.sql.Connection?
Show answer
A compact source file implicitly imports public top-level types exported by java.base. List is in java.base, while Connection is in the separate java.sql module.