Sub topic 12
Restrictions and Common Pitfalls
This chapter assumes you know basic Java syntax, including classes, fields, methods, and the traditional public static void main(String[] args) method. It also assumes you can compile and run Java programs from a command line. The examples use the final Java 25 features for instance main methods and compact source files.
A compact file is still a class
A compact source file looks like a script, but Java does not treat it as a collection of unrelated statements. The compiler creates an implicitly declared class and places the file's fields, methods, and nested types inside that class.
That creates several restrictions:
- The file cannot contain a package declaration.
- The file cannot declare another explicit top-level class or interface.
- Top-level methods and fields become members of the implicit class.
- The file name determines the implicit class name when you launch or compile it.
- Code that depends on a particular package structure may need to be moved into an ordinary class.
You can still declare helper types, such as records, inside the compact file. They become members of the implicit class rather than separate top-level types.
record Point(int x, int y) {}
String format(Point point) {
return "(" + point.x() + ", " + point.y() + ")";
}
void main() {
IO.println(format(new Point(3, 4)));
}Main method selection can surprise you
The Java launcher looks for a launchable main method. If several candidates exist, it prefers them in this order:
| Priority | Method signature | Object needed? |
|---|---|---|
| 1 | `static void main(String[] args)` | No |
| 2 | `static void main()` | No |
| 3 | `void main(String[] args)` | Yes |
| 4 | `void main()` | Yes |
The method must be accessible to the launcher. A private method is not a suitable entry point. If a static method is present, the launcher uses it before an instance method, even if the instance method appears first in the source.
An instance main also requires an accessible no-argument constructor. The launcher creates an object and then calls the method. If you add only a constructor that requires arguments, the program cannot start through that instance method.
class Greeter {
Greeter(String language) {}
void main() {
IO.println("This cannot be launched automatically.");
}
}This class has no no-argument constructor, so the launcher cannot create the required instance.
Instance state has a lifetime
An instance field belongs to the object created for the launch. It is available to main and to other instance methods, but it is not shared automatically with objects created elsewhere.
int count;
void greet() {
count++;
IO.println("Greeting " + count);
}
void main() {
greet();
greet();
}This prints greeting numbers 1 and 2 because both calls use the same launched object. Do not expect count to survive across separate program executions.
A common error is using a static context as if it were an instance context. Top-level methods in a compact file are instance methods, so they can directly use top-level fields and call other top-level methods. If you need static utility behavior, put it in a nested helper type or convert the file into an explicit class.
File names and Java versions matter
Save a compact source file as Greeting.java, then run it with a Java 25 launcher:
java Greeting.javaAn older JDK may report syntax errors because it does not understand compact source files. Earlier releases exposed related features as previews, which required matching preview flags. Do not combine a Java 25 JDK with preview flags from an older release.
When a program grows, convert it to an explicit class. Move the members inside the class body and keep the same instance main method:
public class Greeting {
void main() {
IO.println("Hello");
}
}Exercise
A compact file contains static void main(String[] args) and void main(). Which method does Java launch first?
Show answer
It launches the static method with the String[] parameter because that signature has the highest priority.