Running static main method inside abstract class, containing another main method

Mikhail Pyltsin pyltsinm at gmail.com
Tue Jan 21 12:47:07 UTC 2025


  Hi!

I am investigating a new version of  jep495
(
https://cr.openjdk.org/~gbierman/jep495/jep495-20241101/specs/simple-source-files-instance-main-methods-jls.html#jls-12.1.3
 )
and came up with such an example:
```java
public abstract class AbstractClass {
    public static void main() {
        System.out.println("Hello, World!");
    }

    public void main(String[] args) {
        System.out.println("Hello, World! no constructor, non-static,
args");
    }
}
```
Right now java chooses `void main(String[] args) ` because it contains an
argument, but cannot create this class, because it is abstract,
I have got this error: `Exception in thread "main"
java.lang.InstantiationException: AbstractClass`
Could you help me if this is expected? I expect that `public static void
main()` will be chosen.

I cannot find anything related to this in JEP except this one:
`The behavior of an implementation if there is no candidate method to
invoke, or if there is no suitable constructor in the initial class when
invoking an instance candidate method, is beyond the scope of this
specification.', but it doesn't contain this case.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20250121/119a140f/attachment.htm>


More information about the amber-dev mailing list