Compact Source Files and Instance Main Methods
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
void main() {
IO.println("Hello, World!");
}Hello, World!Compare it with the classic version:
| Classic | Java 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:
- 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.
- An instance
mainmethod.maindoesn't have to bepublicorstatic, and it can leave outString[] args. - The
IOclass.IO.println,IO.printandIO.readlnare short ways to write to and read from the console. They're part ofjava.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.
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);
}Your name: Passengers: Navika, your total is Rs. 1800When you run it yourself, you type Navika and 4 after each prompt, so the screen looks like this:
Your name: Navika
Passengers: 4
Navika, your total is Rs. 1800How it works
- Line 2 prints
Your name:and waits. Whatever you type, up to Enter, comes back as aString. - Line 3 reads a second line and turns it into a number with
Integer.parseInt. - 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.
void main() {
List<String> items = List.of("rice", "dal", "oil");
IO.println("Items: " + items);
IO.println("Count: " + items.size());
}Items: [rice, dal, oil]
Count: 3You'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:
class Counter {
int count = 3;
void main() {
IO.println("Starting count: " + count);
}
}Starting count: 3Because 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
- Easy: Rewrite your Hello World from the earlier lesson as a compact source file and run it with
java Hello.java. - Harder: Write a compact program that asks for a price and a quantity with
IO.readln, and prints the total in rupees.
Key Takeaways
Was this tutorial helpful?