Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8306113

Implementation for JEP 445: Unnamed Classes and Instance Main Methods (Preview)

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 21
    • tools
    • None
    • behavioral
    • low
    • Hide
      1. Classes that declare main methods that aren't valid entry points today may become launchable.

      2. 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.
      Show
      1. Classes that declare main methods that aren't valid entry points today may become launchable. 2. 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.
    • Language construct, add/remove/modify command line option
    • SE

    Description

      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:

      1. 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.

      2. 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:

      1. A static void main(String[] args) (or String... args) method of non-private access (i.e., public, protected or package) access declared in the launched class,

      2. A static void main() method of non-private access declared in the launched class,

      3. A void main(String[] args) (or String... args) instance method of non-private access declared in the launched class or inherited from a superclass, or, finally,

      4. 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.

      Attachments

        Issue Links

          Activity

            People

              rpressler Ron Pressler
              rpressler Ron Pressler
              Jim Laskey, Jonathan Gibbons
              Votes:
              0 Vote for this issue
              Watchers:
              11 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: