Java 25 Compact Source Files and Instance main Methods
Introduction
The traditional first Java program needs a surprising amount of ceremony: a
OUTPUT of the above command
That is a complete program: no class declaration, no
A compact source file with fields and methods
A compact source file can contain any number of methods and fields, which can call and use each other, just like the members of a class -
OUTPUT of the above code
How it works
OUTPUT of the above code
Instance main methods in ordinary classes
Instance main methods are not limited to compact source files: any class can now declare
public class, a method declared public static void main(String[] args), and System.out.println to print a line. Beginners must type concepts - classes, access modifiers, static members, arrays - long before they can understand them, and even experienced developers carry that ceremony into every small script. After four previews from Java 21 to 24, Java 25Click to read tutorial: Overview of New Features in Java 25 (JEP 512) finalized compact source files and instance main methods. A source file can now contain just methods and fields, with a main method that need not be static, public or take any parameters, and a new IO class makes console input and output simple. This tutorial explains them with examples - the simplest programs, how a compact source file works, how the launcher chooses a main method, and how a small program grows into a normal class.
The simplest Java program
Hello World in Java 25
//Hello.java
void main() {
IO.println("Hello, Java 25!");
}
java Hello.java
Hello, Java 25!
public static, no String[] args. Compare it with the traditional Hello WorldClick to read: traditional Hello World, where every line needs explaining. The program runs directly from its source file with the source launcherClick to read tutorial: Java 22: Run Multi-File Programs Without Compiling, or can be compiled with javac as usual.
Fields and methods in a compact source file
//Grades.java
final Map<String, Integer> marks = new TreeMap<>(); // a field: java.util is imported automatically
void main() {
marks.put("Tom", 78);
marks.put("Harry", 91);
marks.put("Sam", 64);
marks.forEach((name, mark) -> IO.println(name + ": " + mark + " -> " + grade(mark)));
IO.println("Average: %.1f".formatted(average()));
}
String grade(int mark) {
return mark >= 90 ? "A" : mark >= 75 ? "B" : "C";
}
double average() {
return marks.values().stream().mapToInt(Integer::intValue).average().orElse(0);
}
Harry: 91 -> A Sam: 64 -> C Tom: 78 -> B Average: 77.7
- A source file whose top level contains methods or fields outside any class is a compact source file. The compiler wraps its contents in an implicitly declared class, named after the file - here
Grades. The class isfinal, has a no-argument constructor, and cannot be referred to by name from other code. - A compact source file automatically imports the whole
java.basemodule, using a module import declarationClick to read tutorial: Java 25 Module Import Declarations with Examples. That is whyMapandTreeMapfromjava.utilcan be used without animport. main()is an instance method: the launcher creates an instance of the implicit class and callsmain()on it, somain()can use the instance fieldmarksand call instance methods likegrade()- nostaticneeded anywhere.
java.lang.IO, new in Java 25, has static methods for the console: println(), print(), and readln(prompt), which prints a prompt and reads a line - replacing the classic Scanner or BufferedReader setup for simple programs. Since IO is in java.lang, it can be used in any class, not only in compact source files -
Reading input with IO.readln
//Greet.java
void main() {
String name = IO.readln("What is your name? ");
int year = Integer.parseInt(IO.readln("Which year were you born? "));
IO.println("Hello " + name + ", you will be " + (2025 - year) + " in 2025.");
}
What is your name? Tom Which year were you born? 1995 Hello Tom, you will be 30 in 2025.
void main(), with or without String[] args, and with any access other than private. When it launches a class, the JVM chooses the main method in this order -
- a
main(String[] args)method - static or instance - is preferred, - otherwise a
main()method with no parameters is used.
public static void main(String[] args) still works exactly as before.
Growing the program
A compact source file is meant as an on-ramp, not a separate dialect of Java. When a program grows, wrapping its contents in class Grades { ... } and adding the imports it uses turns it into an ordinary class, with no other changes. More files can be added and run with the multi-file source launcherClick to read tutorial: Java 22: Run Multi-File Programs Without Compiling, and eventually the program can move to a build tool. Compact source files cannot declare a package, and other classes cannot refer to the implicit class by name.
Summary
In this tutorial we saw how Java 25's compact source files and instance main methods remove the ceremony from small programs. We wrote Hello World as a single void main() method, built a compact source file with fields and methods in an implicitly declared class which automatically imports java.base, and read console input with the new IO class. Lastly, we saw how the launcher chooses a main method, and how a compact source file grows into an ordinary class. Next, read about module import declarationsClick to read tutorial: Java 25 Module Import Declarations with Examples.Continue Reading Java 22 to 25 Tutorials
Overview of New Features in Java 25Click to read tutorial: Overview of New Features in Java 25 Java 25 Module Import Declarations with ExamplesClick to read tutorial: Java 25 Module Import Declarations with Examples Java 25 Flexible Constructor Bodies with ExamplesClick to read tutorial: Java 25 Flexible Constructor Bodies with Examples Java 25 Scoped Values Tutorial with ExamplesClick to read tutorial: Java 25 Scoped Values Tutorial with Examples Java 25 Compact Object Headers ExplainedClick to read tutorial: Java 25 Compact Object Headers Explained Java 25 Key Derivation Function (KDF) API TutorialClick to read tutorial: Java 25 Key Derivation Function (KDF) API Tutorial Overview of New Features in Java 22Click to read tutorial: Overview of New Features in Java 22 Overview of New Features in Java 23Click to read tutorial: Overview of New Features in Java 23 Overview of New Features in Java 24Click to read tutorial: Overview of New Features in Java 24
Overview of New Features in Java 25Click to read tutorial: Overview of New Features in Java 25 Java 25 Module Import Declarations with ExamplesClick to read tutorial: Java 25 Module Import Declarations with Examples Java 25 Flexible Constructor Bodies with ExamplesClick to read tutorial: Java 25 Flexible Constructor Bodies with Examples Java 25 Scoped Values Tutorial with ExamplesClick to read tutorial: Java 25 Scoped Values Tutorial with Examples Java 25 Compact Object Headers ExplainedClick to read tutorial: Java 25 Compact Object Headers Explained Java 25 Key Derivation Function (KDF) API TutorialClick to read tutorial: Java 25 Key Derivation Function (KDF) API Tutorial Overview of New Features in Java 22Click to read tutorial: Overview of New Features in Java 22 Overview of New Features in Java 23Click to read tutorial: Overview of New Features in Java 23 Overview of New Features in Java 24Click to read tutorial: Overview of New Features in Java 24