In a Java compact source file, what class does the compiler declare, and which main method does the launcher run when several forms coexist?
answer
- members outside any class
- final, top level, unnamed package
- its declaration is never in scope
- parameter ranks, static does not
- String[] first, then no arguments
basics
~20 sThe compiler declares a final top-level class in an unnamed package, extending Object, with a default constructor. The launcher picks the main taking String[] if there is one, otherwise the no-argument main; static or instance does not affect the choice.
solid answer
~50 sA file with a method outside any class is a compact source file, and the compiler wraps its members in an implicitly declared class: `final`, top level, in an unnamed package, with direct superclass `Object`, no superinterfaces and a default constructor. `javac` names it after the file, but that declaration is never in scope, so the file cannot `new` it, and JEP 512 says no other code should rely on the name. The file must declare a non-private `void main()` or `void main(String[])`, or it does not compile. At launch, JLS 12.1.4 has two ranks: a `main` taking `String[]` wins, otherwise the no-argument `main` runs. Static versus instance plays no part in the ranking: for an instance `main`, the launcher first creates an object. A static and an instance `main` with the same signature cannot coexist, because that is a duplicate-method compile error.
code
java · 7 linesstatic void main() {
IO.println("static no-arg");
}
void main(String[] args) {
IO.println("instance String[]");
}go deeper
Recall that a Java file with a method outside any class is compact, that the compiler supplies the class around it, and that the file still needs a void main method.
Explain the implicit class's shape: final, top level, unnamed package, extends Object, default constructor. Then give the two-rank main rule, String[] first and then no-argument, and say that static does not rank.
Show you can predict odd launches: a private or non-void main is not a candidate, an inherited String[] main beats a subclass's no-argument main, and an instance main needs a non-private no-argument constructor.
Decide where compact files belong in a team: good for scripts, teaching and small tools, poor for shared code, since other code should not rely on the class's name. Set the point at which a script gets a class and a build.
## What a compact source file is A **compact source file** is a Java source file that declares at least one method, plus any fields or nested types, outside any class. The JLS calls it a *compact compilation unit* (§7.3). A file holding only `class Test { }` stays an ordinary compilation unit, whereas one top-level method makes the whole file compact, and any class or interface in it becomes a member class. ```java String greeting = "Hello"; void main() { IO.println(greeting); } ``` ## The class the compiler declares The compiler wraps the file's members in an **implicitly declared class**. JLS §8.1.8 fixes its shape: | Property | Value | |---|---| | Kind | Top level, `final`, not `abstract` | | Package | A member of an unnamed package, with package access | | Supertypes | Direct superclass `Object`, no direct superinterfaces | | Constructor | A default constructor, and no others | | Members | Every field, method, member class and member interface in the file | | Entry point | At least one candidate `main`, or compilation fails | Several consequences follow: - **Its name is not in scope.** The JLS leaves the name to the host system. `javac` uses the file name without `.java`, so `Hello.java` yields `final class Hello`, and `my-script.java` fails with `bad file name`. Even so, `new Hello()` fails with `cannot find symbol` in that file and in any file compiled with it, because JLS §6.3 gives the implicit declaration an empty scope. A file compiled later against `Hello.class` can name the class, but JEP 512 says no code should rely on that name. Inside an instance method, use `this`. - **Constructors and initializers are out.** The file cannot declare constructors, instance initializers or static initializers. - **So is a package.** A `package` line gives `compact source file should not have package declaration`. - **Nested types stay nested.** `record P(int x) { }` in `Hello.java` becomes the member class `Hello$P`. - **Imports are wider.** The file behaves as if it began with `import module java.base;`, so `List` and `Path` need no import. `IO` is in `java.lang`, and its methods still need the class name, `IO.println` and not `println`, unless you import them statically. - **`javac` checks the entry point.** A file with only `private void main()` or `int main()` fails with `compact source file does not have main method in the form of void main() or void main(String[] args)`. ## How the launcher chooses main A **candidate** is a method named `main` with a `void` result and public, protected or package access, taking either one `String[]` or nothing. It may be static or instance, declared or inherited, and `String...` is the same type as `String[]`. Having several candidates is not an error. JLS §12.1.4 then ranks them on one question only, the parameter: 1. If a candidate takes a `String[]`, the launcher chooses it. 2. Otherwise, if a candidate takes no parameters, the launcher chooses that. 3. Otherwise the launch fails with an error. Only then does `static` matter, and only for how the method is called. A static `main` is invoked directly. For an instance `main`, the launcher first creates an object through the no-argument constructor. | Declared together | Result | |---|---| | `static void main()` and `void main(String[] a)` | The instance `String[]` form runs | | `static void main(String[] a)` and `void main()` | The static `String[]` form runs | | `void main()` and `static void main()` | Compile error: `method main() is already defined` | | `void main(String[] a)` and `static void main(String... a)` | Compile error: `cannot declare both` | | `private void main(String[] a)` and `void main()` | `void main()` runs, because a private method is not a candidate | | `int main(String[] a)` and `void main()` | `void main()` runs, because a non-void method is not a candidate | The static and instance forms of one signature cannot coexist at all, so the ranking never has to choose between them. Both launch styles share the logic: `java Hello` goes through `LauncherHelper` and `java Hello.java` through `SourceLauncher`, and each calls the JDK-internal `MethodFinder.findMainMethod`, whose search never looks at `static`. ## Instance main in an ordinary class The implicitly declared class always meets the instance-launch rules, since it is final and top level with a default constructor. An ordinary class may not, and when the chosen `main` is an instance method the launcher reports an error in each of these cases: - It has no non-private no-argument constructor. - It is abstract. - It is a non-static inner class. Inherited candidates count as well. If `Sub` declares `void main()` and its superclass has `public static void main(String[] args)`, launching `Sub` runs the superclass's method, because `String[]` ranks first. A default `main` from an interface is also a candidate. A compact source file cannot hit these cases, because its class extends only `Object` and implements nothing. ## How the rule got here - **JDK 21, JEP 445:** the first preview, as unnamed classes. Its protocol ranked four forms, static before instance. - **JDK 22, JEP 463:** the second preview replaced that with the two-rank rule above, so the static-first list is out of date. - **JDK 25, JEP 512:** final, after previews in 23 and 24, under the name compact source files. - **JDK 25 against 27:** `MethodFinder` at 27 also rejects a package-private `main` from a superclass in another package. Launching `q.B`, which extends `p.A`, ends in `Main method not found` at 27, while JDK 25's launcher runs that method.
- How did the launcher's ranking of `main` methods change between the first preview in JDK 21 and the final feature?JEP 445 in JDK 21 ranked four forms, static before instance: static `String[]`, static no-argument, instance `String[]`, instance no-argument. JEP 463 in JDK 22 replaced that with two ranks, `String[]` and then no-argument, so `static` no longer decides. JEP 512 finalised the feature in JDK 25, which makes a four-step list out of date.
- In an ordinary class, does a subclass's own `void main()` beat a `public static void main(String[])` inherited from its superclass?No. The rule covers methods declared or inherited, and `String[]` ranks first, so launching the subclass runs the superclass's static `main`. A compact source file cannot hit this, because its implicitly declared class extends only `Object`. A default `main` inherited from an interface is a candidate too.
- What must an ordinary Java class provide when the chosen `main` is an instance method?A non-private constructor with no parameters, because the launcher creates the instance itself. The class also cannot be abstract or a non-static inner class: the launcher reports an error for each before calling `main`. The implicitly declared class of a compact source file always qualifies.
- What changed between JDK 25 and JDK 27 in how the launcher looks up `main`?JDK 27's `MethodFinder` also requires a package-private `main` to come from the launched class's own runtime package. So launching `q.B`, which extends `p.A` with a package-private `main()`, fails at 27 with `Main method not found` and runs at 25. A package-private method is not inherited across packages, so 27 matches JLS 12.1.4.
- The launcher prefers a static main over an instance main.
- One class can declare both static void main() and void main().
- Two candidate main methods in one class are a compile-time error.
- The implicit class is a nested class generated by the launcher.
- A compact source file can create its own class with new and the file name.
- A compact source file can declare a package or a constructor.
- Compact source files
- Java 25pre-release 21
- Instance main methods
- Java 25pre-release 21
Assumes Java 27; compact source files and instance main methods are final since Java 25, and JDK 25's launcher also runs a package-private main inherited across packages.
context
- Chapter 12. Executiondocs.oracle.com
- Chapter 8. Classesdocs.oracle.com
- JEP 512: Compact Source Files and Instance Main Methodsopenjdk.org