Summary
Add flexible main methods and anonymous main classes to the Java language
Problem
The Java language necessitates the explicit use of some programming-in-the-large constructs such as classes and access control even for the simplest of programs. This makes the language unnecessarily difficult to learn for programming beginners, and adds too much noise to small programs.
Solution
This change adds two features:
Flexible main methods allow the main entry point to a program to allow different signatures. In particular, they allow a main method to have no parameters, they allow a main method to have any non-private access, and they allow it to be an instance method. An instance main method can be invoked by the launcher if the launched class has a non-private no-args constructor.
An Anonymous Main Class is an unnamed class that is implicitly provided when a Java file declares one or more methods outside a class declaration. An anonymous main class must have a main method.
Additionally, we deprecate the "inheritance" of today's public static void main(String[])
and have the launcher emit a warning when the launched class's entry point is such a main method declared in a superclass.
Specification
See JEP 445: https://openjdk.org/jeps/445
JLS spec change: https://cr.openjdk.org/~gbierman/jep445/jep445-20230515/specs/unnamed-classes-instance-main-methods-jls.html
The change has no interaction with the JNI invocation API.
The specification of the Main-Class
attribute in the JAR File Specification will change "class name" to "binary class name"; this is not a change of behavior (see https://bugs.openjdk.org/browse/JDK-8308014):
Main-Class: Indicates the binary name of the main application class which the launcher will load at startup. The value of this attribute must not have the
.class
extension appended to the binary name.
(The proposed JLS change in ยง13.1 says: "The binary name of an unnamed top level class (7.3) is any valid identifier (3.8). In simple implementations of the Java SE Platform, where compilation units are stored in files, the binary name of an unnamed top level class would typically be the name of the file containing the unnamed top level class compilation unit (7.3) minus any extension")
The javadoc
tool will fail when asked to generate API documentation for a Java file with an unnamed class, as unnamed classes do not define any API accessible from other classes.
The Class.isSynthetic
method returns true
for an unnamed class.
Selecting a main method
When launching a class, the launch protocol chooses the first of the following methods to invoke:
A
static void main(String[] args)
(orString... args
) method of non-private access (i.e.,public
,protected
or package) access declared in the launched class,A
static void main()
method of non-private access declared in the launched class,A
void main(String[] args)
(orString... args
) instance method of non-private access declared in the launched class or inherited from a superclass, or, finally,A
void main()
instance method of non-private access declared in the launched class or inherited from a superclass.
Note that this is a change of behavior: A public static void main(String[] args)
-- a "traditional" main method -- is not invoked if it is inherited by the launched class. If the launched class inherits a "traditional" main method but the class successfully launches because another main
method (i.e. a declared static no-arg main
or an instance main
) is found, the JVM will issue a warning to the standard error at runtime.
- csr of
-
JDK-8306112 Implementation of JEP 445: Unnamed Classes and Instance Main Methods (Preview)
-
- Resolved
-
- relates to
-
JDK-8302326 JEP 445: Unnamed Classes and Instance Main Methods (Preview)
-
- Closed
-
-
JDK-8308715 Create a mechanism for Implicitly Declared Class javadoc
-
- Resolved
-
-
JDK-8310452 Allow javadoc to process unnamed classes
-
- Closed
-
-
JDK-8319252 CSR JEP 463: JLS Changes for Implicitly Declared Classes and Instance Main Methods (Second Preview)
-
- Closed
-