Java continues evolving to meet the needs of developers—from beginners learning the language to pros writing quick scripts. Java 25 introduces Compact Source Files, a feature that lets you write Java programs without explicit class declarations, coupled with Instance Main Methods which allow entry points that are instance-based rather than static. This combination significantly reduces boilerplate, simplifies small programs, and makes Java more approachable while preserving its power and safety.
What Are Compact Source Files?
Traditionally, a Java program requires a class and a public static void main(String[] args) method. However, this requirement adds ceremony that can be cumbersome for tiny programs or for learners.
Compact source files lift this restriction by implicitly defining a final top-level class behind the scenes to hold fields and methods declared outside any class. This class:
- Is unnamed and exists in the unnamed package.
- Has a default constructor.
- Extends
java.lang.Objectand implements no interfaces. - Must contain a launchable
mainmethod, which can be an instance method (not necessarily static). - Cannot be referenced by name in the source.
This means a full Java program can be as simple as:
void main() {
IO.println("Hello, World!");
}
The new java.lang.IO class introduced in Java 25 provides simple convenient methods for console output like IO.println().
Implicit Imports for Compact Source Files
To keep programs concise, compact source files automatically import all public top-level classes and interfaces exported by the java.base module. This includes key packages such as java.util, java.io, java.math, and java.lang. So classes like List or BigDecimal are immediately available without import declarations.
Modules outside java.base require explicit import declarations.
Limitations and Constraints
Compact source files have some structural constraints:
- The implicit class cannot be named or explicitly instantiated.
- No package declarations are allowed; the class is always in the unnamed package.
- Static members cannot be referenced via method references.
- The
IOclass’s static methods require qualification. - Complex multi-class or modular programs should evolve into regular class-based files with explicit imports and package declarations.
This feature targets small programs, scripts, and educational use while preserving Java’s rigorous type safety and tooling compatibility.
Using Command-Line Arguments in a Compact Source File
Compact source files support standard command-line arguments passed to the main method as a String[] parameter, just like traditional Java programs.
Here is an example that prints provided command-line arguments:
void main(String[] args) {
if (args.length == 0) {
IO.println("No arguments provided.");
return;
}
IO.println("Arguments:");
for (var arg : args) {
IO.println(" - " + arg);
}
}
Save this as PrintArgs.java, then run it with:
java PrintArgs.java apple banana cherry
Output:
textArguments:
- apple
- banana
- cherry
This shows how you can easily handle inputs in a script-like manner without boilerplate class syntax.
Growing Your Program
If your program outgrows simplicity, converting from a compact source file to a named class is straightforward. Wrap methods and fields in a class declaration and add imports explicitly. For instance:
import module java.base;
class PrintArgs {
void main(String[] args) {
if (args.length == 0) {
IO.println("No arguments provided.");
return;
}
IO.println("Arguments:");
for (var arg : args) {
IO.println(" - " + arg);
}
}
}
The logic inside main remains unchanged, enabling an easy migration path.
Conclusion
Java 25’s compact source files paired with instance main methods introduce a fresh, lightweight way to write Java programs. By reducing ceremony and automatically importing core APIs, they enable rapid scripting, teaching, and prototyping, while maintaining seamless interoperability with the full Java platform. Handling command-line arguments naturally fits into this new model, encouraging exploration and productivity in a familiar yet simplified environment.
This innovation invites developers to write less, do more, and enjoy Java’s expressive power with less friction.
Recent Comments