Java 25 makes simple programs much simpler
The Java feature that began as unnamed classes and instance main methods in Java 21 has matured significantly. In Java 25, the feature is final under the name compact source files and instance main methods. It is no longer a preview feature, and you do not need --enable-preview to use it.
The goal is straightforward: let beginners and developers writing small programs use Java without first explaining classes, access modifiers, static, command-line argument arrays and other concepts that are not necessary for the program they are trying to write.
A Java 25 program can therefore start like this:
void main() {
IO.println("Hello, World!");
}That is a complete Java 25 program. There is no explicit class declaration, no public modifier, no static modifier and no String[] args parameter.
Benefits of Java 25 compact source files
- No explicit class declaration is required for a simple program.
- The
mainmethod does not have to bepublic. - The
mainmethod does not have to bestatic. - A zero-argument
main()method is allowed. - The new
IOclass makes simple console input and output concise. - The feature is final in Java 25, so preview flags are no longer required.
The syntax that began as a Java preview feature is finalized in Java 25 as compact source files and instance main methods.
Java 25 compact source file example
The simplest Hello World application needs only an instance main method:
void main() {
IO.println("Hello, World!");
}Compare that with the traditional Java entry point:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}The traditional form remains valid and is still common in existing applications. Java 25 simply gives developers a more compact option when the surrounding class machinery adds no value.
Java 25 instance main methods
The launcher now recognizes more candidate main methods than the classic public static void main(String[] args). In particular, an instance main method can be used as the application entry point.
class HelloWorld {
void main() {
System.out.println("Hello from an instance main method.");
}
}This is useful even when you choose to declare a normal named class. The simpler application startup model is therefore broader than compact source files alone.
The new Java 25 IO class
Java 25 also introduces java.lang.IO to make console interaction less verbose in beginner-oriented programs. Because java.lang is implicitly available, no import is required.
void main() {
var name = IO.readln("What is your name? ");
IO.println("Hello, " + name + "!");
}The compact program can therefore perform both input and output without introducing System.in, System.out, Scanner or stream-handling concepts before they are needed.
Methods and fields in compact source files
A compact source file can contain more than a main method. You can declare fields and helper methods and call them directly from the instance main method.
String greeting = "Hello";
void greet(String name) {
IO.println(greeting + ", " + name + "!");
}
void main() {
greet("Duke");
greet("Java 25");
}This lets a beginner learn variables, methods, control flow and other fundamentals before the course needs to introduce class declarations and static members.
Do compact source files replace Java classes?
No. Compact source files are not intended to eliminate classes or change Java into a classless language. They provide a simpler on-ramp for small programs. As an application grows, developers can introduce named classes, packages, access control, constructors, interfaces and the rest of Java's object-oriented model when those concepts become useful.
The important change is pedagogical as much as syntactic. A developer can now begin with this:
void main() {
IO.println("Hello, World!");
}and learn the traditional form later:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}From Java 21 preview to Java 25 final feature
The terminology and status have changed since the feature first appeared. Java 21 introduced unnamed classes and instance main methods as a preview feature. Subsequent JDK releases refined the design. Java 25 finalizes the work as compact source files and instance main methods.
That distinction matters for older tutorials. In Java 25, developers should not describe this syntax as an experimental Java 21 preview feature, and examples do not require preview compilation or launch options.
Java 25 lowers the barrier to entry
Java's traditional application structure remains available for professional applications of every size, but it no longer has to be the first syntax a new Java developer encounters. Compact source files, instance main methods and the IO class let developers start with executable code and introduce Java's larger object model progressively.
For anyone teaching or learning Java with JDK 25, void main() and IO.println() are now a natural place to begin.