Java Reference ยท Language Features by Version
Simple source files and instance main methods
Reference for Java 25 compact source files and instance main methods: void main(), top-level methods and fields, IO.println, IO.readln and launch rules.
Java 25 lets you write a complete program without the ceremony that used to surround "Hello, World": no public class, no public static void main(String[] args). Two features work together. Instance main methods allow main to be a non-static method, with or without the String[] parameter. Compact source files (also called simple source files) allow methods and fields to sit directly at the top level of the file, with no enclosing class declaration.
Both features are final in Java 25 (JEP 512); before that they were preview features under the names "unnamed classes" (Java 21) and "implicit classes" (Java 22 to 24). They are aimed at learning and scripts. Large applications still use ordinary classes and packages.
void main() { IO.println("Hello, Java 25!"); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Launch rules
When you start a class, the Java launcher looks for a main method in this order and uses the first match:
static void main(String[] args)static void main()void main(String[] args)(instance)void main()(instance)
The method must be non-private and return void. It may be public, protected or package-private. For the instance forms the class needs an accessible, non-private constructor with no parameters; the launcher creates one object and calls main on it.
In the source launcher (java Hello.java) the first top-level class in the file is the one that is started. A compact source file contains no class declaration around its members: the compiler wraps the top-level methods and fields in a final class whose name comes from the file name (our runner names the file Main). Such a file must contain a main method, and you can still declare nested types such as records and enums at the top level of the file; they become members of that implicit class.
What you get in a compact source file
- Automatic imports. Every package exported by
java.baseis imported as if byimport module java.base;. You need explicit imports only for packages outsidejava.base, such asjava.sqlorjava.net.http.import module java.sql;brings in a whole module. - The
IOclass.java.lang.IOhasprint,printlnandreadlnfor the console. Their static methods are imported automatically in a compact file; in an ordinary class writeIO.println(...).IO.readln(prompt)prints the prompt and returns the next line, ornullat end of input.
Usage notes
- Fields and methods at the top level are members of the implicit class, so they can be instance members and share state.
staticis optional; an instancemaincan call instance helpers freely. - Nothing changed for classic programs:
public static void main(String[] args)still works and still has the highest priority. - A compact source file is a single compilation unit. It can use other classes on the class path, but other source files cannot use it.
- In real projects, prefer a named class: tools, frameworks and test runners expect one.