Definition
- Module import (JEP 511) –
import module java.base;imports all public types from a module in one statement, replacing dozens of individual imports. - Compact source files (JEP 512) – a runnable Java file without a
public classdeclaration; the JVM synthesizes the enclosing class. - Instance main –
void main()withoutpublic,static, orString[] argsis valid as the entry point. - Implicit imports – compact source files have implicit
import module java.base;and access toprintln()directly. - Target audience – beginners, scripts, and quick prototypes; production code continues to use explicit classes and targeted imports.
Module import is like telling the bank to give you access to everything in the java.base department instead of requesting each document individually. Compact source files are like writing a quick bank memo without the formal letterhead, for simple internal notes and scripts.
Understanding the Concepts
What are Module Imports and Compact Source Files?
These two features reduce Java's ceremony for small programs and scripts.
What is a module import?
import module java.base; brings in all public packages that the java.base module exports. Instead of writing separate imports for java.util, java.io, java.math, java.time, and others, one line covers everything.
// BEFORE: many individual imports
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.math.BigDecimal;
import java.time.LocalDate;
// AFTER: one module import
import module java.base;
// All of the above (and more) are now available
What is a compact source file?
A compact source file is a Java file that contains just a void main() method with no class declaration. The JVM synthesizes the enclosing class automatically.
// File: BankDemo.java
// No class declaration, no public, no static, no String[] args
void main() {
var accounts = List.of("SAV2001", "CUR3001", "FD4001");
accounts.forEach(id -> println("Processing account: " + id));
}
# Run directly without separate compilation
java BankDemo.java
Processing account: SAV2001
Processing account: CUR3001
Processing account: FD4001
How does the simplest Java program look now?
// Java 1-20: 5 lines of ceremony
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, National Bank!");
}
}
// Java 24+: 3 lines, no ceremony
void main() {
println("Hello, National Bank!");
}
When should you use these features?
| Use case | Module imports | Compact source |
|---|---|---|
| Quick scripts | Yes | Yes |
| Learning/teaching | Yes | Yes |
| Prototyping | Yes | Yes |
| Production services | No (explicit imports) | No (explicit classes) |
| Library APIs | No | No |
What are common mistakes?
// Module imports can cause name conflicts
import module java.base;
import module java.sql;
// Both export java.util.Date and java.sql.Date → ambiguity!
// Fix: use explicit import for the specific type you need
import java.sql.Date;
What should a senior engineer know?
- Module imports are useful in test files and JShell sessions where import lists are noise.
- Compact source files are internally compiled to a class with a synthetic name matching the file.
- The instance main method resolution order:
void main()thenvoid main(String[])thenstatic void main(String[]). - These features do not change the language semantics; they are syntactic sugar for the entry point.
What are common interview follow-up questions?
- Can compact source files access external libraries? (Yes, with the classpath or module path.)
- Does this change how production code is written? (No. Production code uses explicit classes and imports for clarity.)
- Can you have multiple classes in a compact source file? (Yes, but only the synthesized class has the main method.)
Visual Overview
Java Program Evolution:
Java 1:
┌──────────────────────────────────────┐
│ public class Hello { │
│ public static void main(String[] a) │
│ System.out.println("Hello"); │
│ } │
│ } │
└──────────────────────────────────────┘
Java 24+:
┌──────────────────────────────────────┐
│ void main() { │
│ println("Hello"); │
│ } │
└──────────────────────────────────────┘
Key Takeaway
Module Import Declarations (JEP 511) replace dozens of individual imports with one import module java.base;, bringing in all public types from the module. Compact Source Files (JEP 512) simplify small programs by removing the class declaration, allowing an instance main method void main(), and providing direct access to println().
These features target beginners, scripting, and quick prototypes. A complete runnable Java program is now three lines. The java BankDemo.java command runs it directly without separate compilation. For production codebases, explicit imports and class declarations remain preferred for clarity and maintainability.
In a banking context, a quick end-of-day reconciliation script that reads a CSV, sums transaction amounts, and prints a total can be written as a compact source file and run directly. No build tool, no boilerplate, just the logic.
Remember one sentence: module imports and compact source files reduce Java's ceremony for small programs, letting you write void main() with no class declaration and one import for all of java.base.
---