Compact source files and instance main

Java 25 Course · lesson 2 of 16 · 4 min read

After four previews, a Java file no longer needs a class declaration to run.

Open this lesson in the learning hub

Key points

  • JEP 512 finalises what previewed as implicit classes: a file may hold methods with no class wrapper.
  • void main() is enough - no public, no static, no String[] args.
  • The compiler still produces an ordinary class, so the runtime model is unchanged.
  • It also ships IO.println so a first program does not need System.out either.
  • This is aimed at learning and scripting; large applications keep declaring classes as before.

Example

// Java 25 (JEP 512). The ENTIRE file would be these three lines:
//
//   void main() {
//       IO.println("Hello from a compact source file");
//   }
//
// Java 21 requires all of this to say the same thing:
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello from a compact source file");
        System.out.println();
        System.out.println("Words a Java 25 beginner no longer meets on line one:");
        System.out.println("  public, class, static, void, String[], System.out");
    }
}

Compact source files remove the ceremony from lesson one without changing what the JVM runs.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Java 25 Course course, and every lesson in it is listed on the Java 25 Course contents page.