Starting with Java 25 (JEP 512), multiple main method signatures are allowed within a single class. While the JVM follows a strict
selection algorithm to determine the entry point, having more than one main method can lead to significant confusion.
Having multiple main methods reduces code clarity and maintainability. Readers may be unaware of the specific JVM selection rules—such
as the preference for static over instance methods or methods with parameters over those without—and might incorrectly assume a different
method will execute. This ambiguity forces developers to spend unnecessary time deciphering which method actually serves as the program’s entry
point.
Keep only one main method per class. If you require multiple entry points for different execution scenarios, create separate classes
or rename the secondary methods to reflect their actual purpose.
public class Application {
// Noncompliant: Multiple main methods are confusing and lead to shadowing
public static void main(String[] args) {
System.out.println("Static main detected; shadowing instance main.");
}
void main() {
System.out.println("Unreachable entry point due to static precedence.");
}
}
public class LegacyApplication {
// Compliant: Standard static entry point
public static void main(String[] args) {
System.out.println("Running standard static entry point.");
}
}
class Application {
// Compliant: Instance main method in a separate class
void main() {
System.out.println("Running modern instance main entry point.");
}
}