Compact source files and instance main methods
How compact source files and instance main methods evolved from Java 21 preview to Java 25 final, how main selection works and why production Java still uses explicit classes.
On this page
🚪 What are compact source files and instance main methods?
They are a modern way to write a small Java program with less ceremony around the entry point. Instead of starting with public class Hello { public static void main(String[] args) { ... } }, newer Java versions let beginners and script authors write an instance main, and Java 25 finalises compact source files where top-level methods live in an implicit class. The feature changes how tiny programs can start; it does not change how backend services should be structured.
This topic is asked less than collections or inheritance, but it is a good signal that you track Java versions accurately. The trap is saying it is a Java 21 final feature. It is not. Java 21 had the first preview.
What you’ll learn
- State the version history accurately from JEP 445 in Java 21 preview to JEP 512 in Java 25 final
- Explain instance
main, compact source files, the implicit class and the launcher main-selection order - Know what Java 25 adds, including automatic
java.baseimports and theIOhelper class - Answer whether
public static void main(String[] args)is still required in modern Java - Describe safe use cases for teaching, scripts and small demos without changing production architecture
For this lesson, we'll focus on the version timeline, instance main signatures, compact source files, launcher selection order, the implicit class, Java 25 java.base imports and IO, use cases and the interview answer to "is public static void main still required?"
Below the line (out of scope)
- For Java 21 code, this is preview and needs preview flags.
- For Java 25, JEP 512 finalises the feature under the name compact source files and instance main methods.
- We are not changing package design, modules or framework startup patterns.
The 30-second answer
public static void main(String[] args) is still valid, but it is no longer the only source-file entry point in modern Java. The feature started as JEP 445 in Java 21 preview, continued as JEP 463 in Java 22 preview, JEP 477 in Java 23 preview, JEP 495 in Java 24 preview, and was finalised by JEP 512 in Java 25 as compact source files and instance main methods. The launcher looks for candidate main methods in a defined order, preferring static void main(String[] args), then static void main(), then instance forms. In a compact source file, top-level methods are wrapped in an implicit class. Java 25 also makes simple programs friendlier with automatic imports from java.base and a simple IO helper class. This is mainly for learning, demos and small scripts, not a reason to remove explicit classes from production services.
Interview radar
Asked very often: Is public static void main(String[] args) mandatory, and what changed in recent Java versions?
Senior rounds probe: Which versions were preview versus final, and how does the launcher choose among overloaded main methods?
Production angle: Teams should not rewrite service entry points for this; use it for teaching, samples and small command-line programs.
01Picture it first: lowering the first step, not changing the staircase
Think of Java startup like entering a building. The traditional entrance is still there: a named public class with a public static main(String[] args). The newer feature adds a smaller side door for first programs and tiny tools. Once you are inside a real application, the same rooms exist: classes, methods, packages, modules and frameworks.
The diagram below shows the version timeline, because that is the interview trap.
Compact source files and instance main methods progressed from Java 21 preview through Java 24 preview and became final in Java 25 as JEP 512
If the interviewer says "in Java 21", your answer must include "preview". Java 21 finalised pattern switch and record patterns, but not compact source files.
02Instance main methods under the hood
Try it yourself first
Which of these entry points can the launcher use in the modern feature line?
Premium lesson
Keep reading with the Java Pass
The rest of this lesson is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
- Real world: demos are not service architecture
- Advanced: implicit classes, imports, IO and preview status
- How to say it in the interview
From ₹199 · one-time payment · No auto-renewal
Further reading (7)
- JEP 445: Unnamed Classes and Instance Main Methods
- JEP 463: Implicitly Declared Classes and Instance Main Methods
- JEP 477: Implicitly Declared Classes and Instance Main Methods
- JEP 495: Simple Source Files and Instance Main Methods
- JEP 512: Compact Source Files and Instance Main Methods
- Oracle Java 21 java launcher documentation
- Baeldung: Java Main Method