Write Java without public static void main
Compact source files and instance main methods: the smallest Java program is now three lines.
void main() {
IO.println("Hello, Java 25!");
} Run with JDK 25: java CompactHello.java
Java 25 finalizes compact source files and instance main methods. A beginner no longer has to meet classes, public, static and String[] on day one.
Before and after
| Before | Java 25 | |
|---|---|---|
| Class declaration | Required | Optional |
main signature | public static void main(String[] args) | void main() |
| Print to console | System.out.println | IO.println |
| Run it | javac then java | java Hello.java |
Output
Hello, Java 25!
When to use it
- Learning, scripts, demos and short examples like the ones on this site.
- Not for large applications: once a file needs several classes or a package, use a normal class.
Every example on java25.com is a single file you can run with java FileName.java on JDK 25.