J.IX · Java 25 practice question

A developer writes a compact source file containing only a helper method and a field, with no main method. What happens at compile time in Java 25?

Choose one answer
Check answer and explanation

Correct answer: A compile-time error is reported because a compact source file must have a launchable main method

Explanation

The implicitly declared class of a compact source file must have a launchable main method; if it does not, the compiler reports a compile-time error.

Common trap

Normal top-level classes do not require a main method, so developers may expect a compact source file to compile as a library class. The compiler synthesizes a default no-parameter constructor for a compact source file, not a main method.

Source: JEP 512: Compact Source Files and Instance Main Methods — Description
Checked against the cited text on 2026-11-25.

Report an error in this question