Instance main method and vararg constructor

Tagir Valeev amaembo at gmail.com
Fri Jul 5 15:05:16 UTC 2024


Hello!

I wonder if we should enable instance main method if the class has only
vararg constructor:

class Test {
  Test(String... data) {
  }

  void main() {
    System.out.println("Hello World!");
  }
}

Java 22 doesn't like it:
> \jdks\jdk-22\bin\java.exe --enable-preview Test.java
error: can't find no argument constructor in class: Test

However, in other contexts, such a class is considered as a class that has
no-arg constructor. E.g., we can subclass it without explicit
super-constructor invocation:

class Test {
  Test(String... data) {
  }

  static class Use extends Test {}
}

Would it be consistent to allow launching such classes?

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


More information about the amber-spec-experts mailing list