Compact source files in Java
Java 25 instance main methods: void main() { ... } without a class declaration for small programs.
main? In Java 25, a complete program can be just three lines. Where did everything go?The shortest Java program
No class, no public static, no String[] args. Java 25 finalized compact source files (JEP 512): a file can hold just methods and fields, with a simple void main() as the entry point. IO.println prints a line.
void main() {
IO.println("Hello, Java 25!");
}The class is still there
It's still ordinary Java. The compiler wraps your top-level methods and fields in an implicitly declared final class. main is an instance method: the launcher creates an object of that class and calls main on it.
Your turn
A field, a method and main. What does this print?
int bonus = 10;
int score(int base) {
return base + bonus;
}
void main() {
IO.println(score(5));
}15Compile error5
Show the answer
15. bonus and score are members of the same implicit class, and the launcher already made an instance, so main can use them directly.
Free imports
Compact source files automatically import every package of the java.base module: java.util, java.io, java.math and more. So ArrayList, List and Map work with no import line. IO.println and IO.readln keep console input and output simple.
Your turn
There's no import for ArrayList. What does this print?
void main() {
var names = new ArrayList<String>();
names.add("Ada");
names.add("Linus");
IO.println(names);
}[Ada, Linus]Compile error["Ada", "Linus"]
Show the answer
The java.base imports cover java.util.ArrayList, so it compiles. A list prints as [element, element] without quotes.
Nobody else can call it
The implicit class has no name your code can use. Another class can't call Main.score(5). Compact files are for small standalone programs; when you need reuse, turn it into a normal named class.
In real projects
Scripts, coding puzzles, teaching and quick tools: compact files plus java File.java make Java feel like a scripting language. When the script grows up, wrapping it in a class is a small step, because it's the same language.
Key takeaways
- void main() can be an instance method with no String[] parameter
- Top-level methods and fields go into an implicit final class
- All java.base packages are auto-imported (java.util, java.io…)
- IO.println / IO.readln make console I/O simple
This feature had four preview rounds. It debuted in Java 21 as "Unnamed Classes and Instance Main Methods" (JEP 445) and was finalized in Java 25.
Practice questions
What does this print?
String greet(String name) {
return "Hi, " + name;
}
void main() {
IO.println(greet("Ada"));
}- Compile error
- greet(Ada)
- Hi, name
- Hi, Ada
Check your answer
Hi, Ada. Both methods are members of the same implicitly declared class. The launcher creates an instance and calls main, which can call greet directly.
What does the compiler do with the top-level methods in a compact source file?
- Interprets them line by line like a shell script
- Wraps them in an implicitly declared final class
- Makes every method static automatically
- Places them in the java.lang package
Check your answer
Wraps them in an implicitly declared final class. It's still ordinary Java: the members become part of a final class that the compiler declares for you, named after the file.