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

Tagir Valeev amaembo at gmail.com
Fri Jan 31 12:49:31 UTC 2025


Hello!

The following message related to JEP 495 "Simple Source Files and Instance
Main Methods" was posted by my colleague, Mikhail Pyltsin, to compiler-dev,
but probably it belongs better to amber-dev. Mikhail told me that he has
trouble posting to amber-dev. According to him, messages go to the
moderation queue and never appear on the list. Now, I'm reposting his
message under my name, as the problem looks somewhat important to me. The
original message follows.

Hi!
Sorry, I am not sure that it is a correct channel, but probably, it is
related to jep specification.
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
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.


With best regards,
Tagir Valeev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20250131/d31e9e32/attachment.htm>


More information about the amber-dev mailing list