☕ Java Foundations · Beginner

Compact source files in Java

Java 25 instance main methods: void main() { ... } without a class declaration for small programs.

🧩 The mysteryRemember all that ceremony around main? In Java 25, a complete program can be just three lines. Where did everything go?

The shortest Java program

No class, no public static, no String[] args. Java 25 finalized compact source files (JEP 512): a file can hold just methods and fields, with a simple void main() as the entry point. IO.println prints a line.

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

The class is still there

It's still ordinary Java. The compiler wraps your top-level methods and fields in an implicitly declared final class. main is an instance method: the launcher creates an object of that class and calls main on it.

🔮 Predict it

Your turn

A field, a method and main. What does this print?

int bonus = 10;
 
int score(int base) {
    return base + bonus;
}
 
void main() {
    IO.println(score(5));
}
  1. 15
  2. Compile error
  3. 5
Show the answer

15. bonus and score are members of the same implicit class, and the launcher already made an instance, so main can use them directly.

Free imports

Compact source files automatically import every package of the java.base module: java.util, java.io, java.math and more. So ArrayList, List and Map work with no import line. IO.println and IO.readln keep console input and output simple.

🔮 Predict it

Your turn

There's no import for ArrayList. What does this print?

void main() {
    var names = new ArrayList<String>();
    names.add("Ada");
    names.add("Linus");
    IO.println(names);
}
  1. [Ada, Linus]
  2. Compile error
  3. ["Ada", "Linus"]
Show the answer

The java.base imports cover java.util.ArrayList, so it compiles. A list prints as [element, element] without quotes.

⚠️ The trap

Nobody else can call it

The implicit class has no name your code can use. Another class can't call Main.score(5). Compact files are for small standalone programs; when you need reuse, turn it into a normal named class.

💼 In the real world

In real projects

Scripts, coding puzzles, teaching and quick tools: compact files plus java File.java make Java feel like a scripting language. When the script grows up, wrapping it in a class is a small step, because it's the same language.

Key takeaways

  1. void main() can be an instance method with no String[] parameter
  2. Top-level methods and fields go into an implicit final class
  3. All java.base packages are auto-imported (java.util, java.io…)
  4. IO.println / IO.readln make console I/O simple
🤯 Did you know?

This feature had four preview rounds. It debuted in Java 21 as "Unnamed Classes and Instance Main Methods" (JEP 445) and was finalized in Java 25.

Practice questions

What does this print?

String greet(String name) {
    return "Hi, " + name;
}
 
void main() {
    IO.println(greet("Ada"));
}
  1. Compile error
  2. greet(Ada)
  3. Hi, name
  4. Hi, Ada
Check your answer

Hi, Ada. Both methods are members of the same implicitly declared class. The launcher creates an instance and calls main, which can call greet directly.

What does the compiler do with the top-level methods in a compact source file?

  1. Interprets them line by line like a shell script
  2. Wraps them in an implicitly declared final class
  3. Makes every method static automatically
  4. Places them in the java.lang package
Check your answer

Wraps them in an implicitly declared final class. It's still ordinary Java: the members become part of a final class that the compiler declares for you, named after the file.

Next: why is there a new Java every six months, and which versions do companies actually bet on?