Sub topic 13
From Preview to Final: Version History
What you need to know first
Assume that you know Java variables, methods, classes, and the traditional public static void main(String[] args) method. You should also know how to run javac and java from a terminal.
Compact source files and instance main methods were introduced gradually. For several Java releases, they were preview features. A preview feature is an experimental language or library feature that developers can try before the Java platform commits to its final design. Preview features can change between releases.
The feature became final in Java 25. If you use Java 25 or newer, you can write compact source files and instance main methods without preview flags.
The path from preview to final
The first version appeared in Java 21, released in 2023. Java 21 used the name unnamed classes and instance main methods. The goal was to make a small Java program easier to read and run while preserving the Java language and virtual machine model.
The feature was previewed again in each of the next releases:
| Java release | JEP | Status and name |
|---|---|---|
| Java 21 | 445 | First preview: unnamed classes and instance main methods |
| Java 22 | 463 | Second preview |
| Java 23 | 477 | Third preview: implicitly declared classes and instance main methods |
| Java 24 | 495 | Fourth preview: simple source files and instance main methods |
| Java 25 | 512 | Final feature: compact source files and instance main methods |
The changing names show that the design was still being refined. Java 25 uses compact source file for a source file whose members appear without an explicit class declaration. The compiler still creates a class, but the programmer does not have to write the surrounding class syntax.
What changed during preview
The basic direction stayed stable:
- A small source file could contain a
mainmethod without a declared class. - The launcher could call an instance
mainmethod. - The runtime could create the object before calling that method.
- Small programs could use simple console I/O through
IO. - Helper declarations, such as records, could appear in the same source file.
The details became more precise over time. The Java team adjusted which method signatures the launcher accepts, how the implicit class is named, which packages receive automatic imports, and how a compact source file interacts with other language features.
This is why code written for an early preview may require changes when compiled with a later JDK. Preview code is useful for learning and experimentation, but it should not be treated as a permanent compatibility contract.
Running preview code versus final code
With a preview release, both compilation and execution generally require --enable-preview. The --release option tells javac which Java platform version the source targets.
For example, a Java 24 preview program can be compiled and run like this:
javac --enable-preview --release 24 Hello.java
java --enable-preview HelloThe exact command must match the installed JDK. Compiling with Java 24 and running with Java 21 cannot work because the older runtime does not know the newer class-file or language behavior.
With Java 25, the feature is final, so no preview flag is needed:
javac Hello.java
java HelloThe source launcher also works:
java Hello.javaThe final release makes these features suitable for ordinary teaching examples and small utilities. It does not make every program better as a compact source file. Larger applications still benefit from explicit classes, packages, and a project structure.
Exercise
You have a compact source file that worked with Java 24 using preview syntax. What should you check before running it with Java 25?
Show answer
Check that Java 25 is installed, remove preview flags if the feature is now final, and test the source because preview behavior or accepted syntax may have changed between releases.