Sub topic 13
From preview to final: version history
This chapter traces how instance main methods and compact source files went from a Java 21 preview to a final feature in Java 25. It covers what changed at each step, why the names changed, and how to read older articles and code. It assumes you know the traditional public static void main(String[] args) and have used javac and java from the command line.
Why the history matters
Most tutorials, blog posts, and Stack Overflow answers about this feature were written during the preview period. They use names, flags, and APIs that no longer match the final feature. If you copy such an example into a Java 25 project, it may fail to compile, or it may compile but behave differently.
Knowing the history lets you recognize which version an example was written for. It also explains why the final feature looks the way it does.
What a preview feature is
A preview feature is fully specified and implemented, but it is not yet permanent. The JDK ships it so developers can try it and send feedback. Three rules apply while a feature is in preview:
- You must opt in with
--enable-preview. Without it,javacandjavareject the feature. - Source-launcher runs also need
--source N, where N is the current release number. - Class files compiled with preview features are tied to the exact Java release that compiled them. They will not run on a different release.
Because of the last rule, preview features are not meant for production code. The design can also change between previews, and this feature changed several times.
Timeline of the feature
The feature was previewed four times before it became final.
Java 21: first preview (JEP 445)
JEP 445 introduced the two core ideas. A class could declare an instance main method, and a source file could contain top-level methods and fields. The compiler wrapped those members in an unnamed class.
There was no console helper class yet. You printed with System.out.println, as in a normal program. A Java 21 file looked like this:
void main() {
System.out.println("Hello, world!");
}You had to opt in explicitly to run it:
java --enable-preview --source 21 Hello.javaJava 22: second preview (JEP 463)
The name changed from unnamed class to implicitly declared class. The old name clashed with the unrelated "unnamed classes" idea, and it suggested the class had no name at all. In fact the compiler gives it a name derived from the file.
The launch rules also became simpler. The launcher looked for a main method with a fixed order of preference, which the next section describes.
Java 23: third preview (JEP 477)
Java 23 added a small console class so beginners no longer needed System.out or Scanner. In this release the class was java.io.IO. Implicitly declared classes automatically imported its static methods, so you could write a bare println:
void main() {
println("Hello, world!");
}This is the form you will see in many articles from late 2024. It does not compile on Java 25 without changes, as explained below.
Java 24: fourth preview (JEP 495)
Java 24 renamed the feature again, to simple source files, and renamed the generated class accordingly. The behavior stayed close to Java 23. The aim was to describe the feature as what it is for, which is writing small programs in a single file, instead of describing a compiler mechanism.
Java 25: final (JEP 512)
Java 25 made the feature permanent and settled on the name compact source files. Three changes from the previous preview matter in practice:
- The console class moved from
java.io.IOtojava.lang.IO. Everything injava.langis already imported, soIOneeds no import in any class, not just compact source files. - The automatic static import of the IO methods was dropped. You now write
IO.println(...)andIO.readln(...), as the earlier chapters did. - Compact source files automatically import the packages exported by the
java.basemodule. This is whyListworks without an import statement.
Java 25 is also a long-term support release, so the feature is available in the version most teams adopt next.
Comparing the versions side by side
The same program is spelled differently in each version.
| Version | Status | Feature name | Console output in a minimal file | Flags needed |
|---|---|---|---|---|
| Java 21 | Preview | Unnamed classes | `System.out.println(...)` | `--enable-preview --source 21` |
| Java 22 | Preview | Implicitly declared classes | `System.out.println(...)` | `--enable-preview --source 22` |
| Java 23 | Preview | Implicitly declared classes | `println(...)` (from `java.io.IO`) | `--enable-preview --source 23` |
| Java 24 | Preview | Simple source files | Check the Java 24 documentation; it follows the Java 23 design | `--enable-preview --source 24` |
| Java 25 | Final | Compact source files | `IO.println(...)` (from `java.lang.IO`) | None |
The Java 24 row is hedged on purpose. Treat Java 24 as a transitional release and read its release notes if you must support it.
The launch protocol
The launcher decides which method to run as the program entry point. When a class declares several candidates, it picks the first match in this order:
static void main(String[] args)static void main()void main(String[] args)(instance)void main()(instance)
Static methods win over instance methods, and a method that takes String[] wins over one that takes no parameters. An instance main must be non-private. The launcher also needs an accessible no-argument constructor to create the instance. If a class has both a traditional main and an instance main, the traditional one is chosen, so old programs keep working unchanged.
Migrating preview code to Java 25
Suppose you have a Java 23 preview file:
String greeting = "Hello";
void main() {
println(greeting + ", preview!");
String name = readln("Name: ");
println("Nice to meet you, " + name);
}On Java 25 you make two kinds of edits. You qualify each IO call with IO., and you drop the preview flags:
String greeting = "Hello";
void main() {
IO.println(greeting + ", final!");
String name = IO.readln("Name: ");
IO.println("Nice to meet you, " + name);
}Run it with:
java Hello.javaCommon errors when versions are mixed
These errors usually mean the source and the JDK come from different points in the timeline.
- "Unnamed classes are a preview feature and are disabled by default" (Java 21) or a similar message mentioning implicitly declared classes (Java 22 to 24). The code uses the feature on a JDK where it is still preview. Add
--enable-previewand--source N, or upgrade to Java 25. - "class file uses preview features of Java 23" when running a compiled class on another release. Classes built with
--enable-previewonly run on the same release, also with--enable-preview. Recompile on the target JDK, or compile without preview on Java 25. - "cannot find symbol: method println(String)" on Java 25. The code was written for the Java 23 preview, which imported
printlnstatically. UseIO.println. - "package java.io.IO does not exist" or an unresolved
java.io.IOimport. The class moved tojava.langin Java 25. Remove the import, becauseIOis available automatically. - Preview flags ignored or flagged on Java 25. The flags still work on the command line, but they are not needed, and they would mark the class files as preview-dependent. Remove them.
Choosing a version for your own work
Use these rules of thumb:
- Learning or teaching: Use Java 25 or later. You get the final names, no flags, and a stable API.
- Production code that must stay on an older LTS release: The feature is not available in a supported form. Write ordinary classes with a
static void main(String[] args). - Experiments on a preview release: Pin the exact JDK version in your notes and scripts, and expect to revise the code when you upgrade.
Programs that use a traditional main method need no changes on any version. The history here only affects code that adopted the new features early.
Exercise
A colleague sends you a Java 23 preview file that calls println("Hi") and readln("Name: ") without any prefix, and it is launched with java --enable-preview --source 23 Hi.java. List the changes needed to run it on Java 25 and give the new launch command.
Show answer
Add the IO. prefix to each call, so they become IO.println("Hi") and IO.readln("Name: "). Java 25 no longer imports the IO methods statically, and the class is now java.lang.IO, so no import is needed. Remove --enable-preview and --source 23. The launch command becomes java Hi.java.
Exercise
A class declares both static void main(String[] args) and void main(). Which one does the launcher run on Java 25, and why?
Show answer
It runs static void main(String[] args). The launch order prefers static methods over instance methods and a String[] parameter over no parameters, so the traditional entry point has the highest priority. This keeps existing programs working.
Exercise
You compiled a file with javac --enable-preview --release 23 on Java 23 and try to run the class on Java 25. What happens, and what is the fix?
Show answer
The launch fails because a class file built with preview features only runs on the exact release that built it. The fix is to recompile on Java 25 without the preview flags, after changing the source to the final syntax.