Compact Source Files and Instance Main Methods

Naveen AnamaneniUpdated · 5 min read

Look at Hello World again: public, class, static, String[] args. That's a lot of words to explain before a beginner has printed a single line. Java 25 made this simpler. A small program can now be just a main method, with no class around it and no static. It's still real Java, compiled and run by the same JVM.

Hello World, the Short Way

Hello.java▶ Try it
void main() {
    IO.println("Hello, World!");
}
Output
Hello, World!
What a compact source file leaves out

Compare it with the classic version:

ClassicJava 25 compact source file
public class Hello {(no class needed)
public static void main(String[] args) {void main() {
System.out.println("Hello, World!");IO.println("Hello, World!");

Three things changed:

  1. No class declaration. A file whose methods sit at the top level is a compact source file. Java creates a class for it behind the scenes, named after the file.
  2. An instance main method. main doesn't have to be public or static, and it can leave out String[] args.
  3. The IO class. IO.println, IO.print and IO.readln are short ways to write to and read from the console. They're part of java.lang, so they work in every Java 25 program, not only compact ones.

Reading Input with IO

IO.readln prints a prompt and reads one line typed by the user. Here's a small fare calculator.

FareCalc.java▶ Try it
void main() {
    String name = IO.readln("Your name: ");
    int people = Integer.parseInt(IO.readln("Passengers: "));
    int fare = 450 * people;
    IO.println(name + ", your total is Rs. " + fare);
}
Output
Your name: Passengers: Navika, your total is Rs. 1800

When you run it yourself, you type Navika and 4 after each prompt, so the screen looks like this:

Terminal
Your name: Navika
Passengers: 4
Navika, your total is Rs. 1800

How it works

  1. Line 2 prints Your name: and waits. Whatever you type, up to Enter, comes back as a String.
  2. Line 3 reads a second line and turns it into a number with Integer.parseInt.
  3. Lines 4 and 5 work out and print the fare.

The output block above shows everything on one line because the typed answers aren't part of the program's output. Only the prompts and the result are.

Try it yourself: add a third question, IO.readln("From city: "), and include the city in the last line.

Common Classes Are Already Imported

A compact source file automatically imports the commonly used parts of the standard library (all of the java.base module), so you can use List, Map or LocalDate without writing import lines.

Shopping.java▶ Try it
void main() {
    List<String> items = List.of("rice", "dal", "oil");
    IO.println("Items: " + items);
    IO.println("Count: " + items.size());
}
Output
Items: [rice, dal, oil]
Count: 3

You'll learn List properly in the collections lessons, later in the course. For now, notice that it works with no import.

Instance Main Methods in a Normal Class

The simpler main also works inside an ordinary class. The launcher creates an object of the class and then calls main on it:

Counter.java▶ Try it
class Counter {
    int count = 3;
 
    void main() {
        IO.println("Starting count: " + count);
    }
}
Output
Starting count: 3

Because main isn't static here, it can use the field count directly. If a class has both the classic public static void main(String[] args) and a simpler main, the classic one is chosen.

Common Mistakes

Running it on an old JDK. On Java 17 or 21 without preview flags, void main() at the top level fails to compile. Check java -version first.

Thinking it's a different language. It's the same Java. Everything you learn later, such as variables, loops and classes, works inside a compact source file.

Expecting other classes to use it. The class Java creates for a compact file has no name you can refer to. Shared code belongs in a normal class.

Practice

  1. Easy: Rewrite your Hello World from the earlier lesson as a compact source file and run it with java Hello.java.
  2. Harder: Write a compact program that asks for a price and a quantity with IO.readln, and prints the total in rupees.

Quick check: Compact source files

4 questions
1Which Java version made compact source files a final feature?
2Which is a valid complete program in Java 25?
3What does IO.readln("Age: ") do?
4A class has both public static void main(String[] args) and void main(). Which runs?

Key Takeaways

Was this tutorial helpful?

Lesson 17 of 482