Skip to content
Tutorials Center
My learning

Topics

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.

Java
void main() {
    IO.println("Hello, Java 25!");
}

Try it

Java A compact source file

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.

Java Instance main in an ordinary class

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:

  1. static void main(String[] args)
  2. static void main()
  3. void main(String[] args) (instance)
  4. 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.base is imported as if by import module java.base;. You need explicit imports only for packages outside java.base, such as java.sql or java.net.http. import module java.sql; brings in a whole module.
  • The IO class. java.lang.IO has print, println and readln for the console. Their static methods are imported automatically in a compact file; in an ordinary class write IO.println(...). IO.readln(prompt) prints the prompt and returns the next line, or null at 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. static is optional; an instance main can 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.