Quick revision
The short answer
Since Java 25 the smallest hello world is a file Hello.java containing only void main() { IO.println("Hello"); }. The compiler wraps those members in an implicitly declared final class in the unnamed package, which no other code can refer to by name, and the file automatically imports every package exported by the java.base module. We run it directly with java Hello.java, or compile it with javac and run it with java Hello.
What to remember
In this answer, we saw that Java 25 allows a whole program to be void main() { IO.println("Hello"); } in a file called Hello.java. The compiler wraps the top-level members in an implicitly declared final class in the unnamed package that other code cannot name, the file imports the whole java.base module automatically, and java.lang.IO gives simple console methods. We ran it with the source launcher and with javac, saw that the launcher calls an instance main on a new object, and noted that compact files are for learning and small scripts, while bigger programs grow into normal classes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Since Java 25 the smallest hello world is a file Hello.java containing only void main() { IO.println("Hello"); }. The compiler wraps those members in an implicitly declared final class in the unnamed package, which no other code can refer to by name, and the file automatically imports every package exported by the java.base module. We run it directly with java Hello.java, or compile it with javac and run it with java Hello.
Why this question is asked
For almost thirty years, the first Java program anyone wrote needed a public class, a public static void main(String[] args) method and System.out.println, five ideas before the first line of output. JEP 512, final in Java 25, removed that ceremony for small programs. Interviewers ask about it because it is a visible language change in the current LTS release, and because explaining it well shows we understand what a class, a package and an import really are. In this answer, we will write the smallest program, see what the compiler turns it into, look at the automatic imports and the new IO class, run it two ways, and discuss where these files fit.
1The Smallest Program in Java 25
Here is the complete content of a file called Hello.java. There is no class declaration, no public keyword, no static and no parameter.
Three things are new at once. First, the file has no class around the method; such a file is called a compact source file. Second, main is an instance method, one that belongs to an object rather than to the class, and it takes no String[] parameter. Third, IO is a class in java.lang, the package every Java file sees without imports, with simple static methods for the console: print, println and readln. In the Java 23 and 24 previews, IO lived in java.io and its methods were imported automatically so we could write println alone; in the final Java 25 version IO is in java.lang and we write IO.println.
2Which Class Does main Belong To?
Every method in Java still belongs to a class, and compact source files do not change that rule. When javac sees fields and methods at the top level of a file, it creates an implicitly declared class around them. That class is final, so nothing can extend it, it extends Object, it lives in the unnamed package, the package used by files with no package statement, and it gets a name chosen by the compiler from the file name, here Hello.
Notice the important restriction: other code cannot refer to that class by name. We cannot write new Hello() in another file or call Hello.something(). The class exists only to be launched. That is a deliberate design choice, because it keeps compact files as entry points and stops them from quietly becoming libraries that other code depends on.
- Hello.javatop-level fields and methods, no class declaration
- import module java.baseadded automatically:
java.util,java.time,java.ioand more - final class Helloimplicitly declared, unnamed package, extends Object
- void main()instance main, called on a new Hello object by the launcher
4 layers, from the top down. Step through them one at a time.
Fields work as ordinary instance fields, and other methods we write become ordinary methods of the same class, so main can call them directly without static.
3What Is Imported Automatically
The example above used LocalDate without any import, and that compiles. A compact source file behaves as if it started with import module java.base;, the module import declaration that Java 25 also finalised. It imports every public type in every package that the java.base module exports. In practice that is java.util, java.util.function, java.util.stream, java.time, java.io, java.nio.file, java.math, java.net and many more.
Types from other modules, such as java.sql.Connection or java.net.http.HttpClient, still need an import, either a normal one or another module import such as import module java.sql;. And any explicit import we write wins over the automatic ones, so name clashes stay under our control.
4Running the Program
There are two ways to run Hello.java, and both work with an instance main.
The first form uses the source launcher, introduced in Java 11 and extended in Java 22 to programs spread across several files. It compiles the file in memory, without writing a .class file, and then runs it. The second form is the classic compile and run. In both cases the launcher sees that main is an instance method, creates an object of the implicit class through its no-argument constructor, and calls main on it. Classic classes with public static void main(String[] args) still run exactly as before.
5Where Compact Source Files Fit
These files are aimed at two groups. Learners can start with statements and methods and meet classes, packages and access modifiers later, when they need them. Experienced developers get a quick way to write small scripts and experiments. The feature does not create a separate dialect: the body of a compact file is normal Java, and growing it into a full program means wrapping the members in a class declaration, adding a package and, if we like, making main static and public again. Larger programs with several types, tests and dependencies should be normal classes in a normal build.
6Follow-up questions
What happens when the program needs a second class?
There are two ways to grow. Inside the compact file we can declare more types, such as a record Point(int x, int y), and they become nested members of the implicit class, usable from main. When the program needs classes in other files, we declare a normal named class in its own file; since Java 22 the source launcher can compile other source files it finds from the launched file's directory. What we cannot do is reference the implicit class itself from those files, so at that point it is usually time to turn the compact file into a normal class and move to a build tool.
Can a compact source file have a package statement?
No. The implicit class always lives in the unnamed package, so a package declaration is not allowed. This is one of the signals that the file is an entry point and not part of a library.
Is System.out.println still allowed?
Yes, nothing old was removed. IO.println is simply shorter and easier to explain to beginners, and IO.readln avoids the old Scanner or BufferedReader setup for reading a line. Both styles can be used in compact files and in ordinary classes.
7Conclusion
In this answer, we saw that Java 25 allows a whole program to be void main() { IO.println("Hello"); } in a file called Hello.java. The compiler wraps the top-level members in an implicitly declared final class in the unnamed package that other code cannot name, the file imports the whole java.base module automatically, and java.lang.IO gives simple console methods. We ran it with the source launcher and with javac, saw that the launcher calls an instance main on a new object, and noted that compact files are for learning and small scripts, while bigger programs grow into normal classes.
Open with
In Java 25, with JEP 512 final, the smallest program is a file called Hello.java that contains just void main, with IO.println of Hello inside. There is no class declaration, no static and no String array parameter.
Then, if they want more
Every method still needs a class, so the compiler wraps the top-level fields and methods in an implicitly declared class. It is final, it is in the unnamed package, its name comes from the file name, and other code cannot refer to it by name, so it can only be launched.
A compact source file also implicitly imports the java.base module, which means everything java.base exports, like java.util, java.time and java.nio.file, is available without import statements. Console input and output come from the IO class in java.lang, which has println, print and readln. In the final version we call IO.println explicitly; the automatic static import existed only in the previews.
To run it I can use the source launcher, java Hello.java, which compiles in memory, or compile with javac and run java Hello. Because main is an instance method, the launcher creates an object with the no-argument constructor and calls main on it.
I would use this for learning and small scripts, and move to normal classes once a program grows.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 22, Java 11, Java 23
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Java 25 finalised compact source files and instance main methods (JEP 512). Show the...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/270e572204/