Missing warning when using an instance main method

Brian Goetz brian.goetz at oracle.com
Mon Sep 18 17:17:36 UTC 2023


I think I'm closer to Tagir on this one.  We have never tried to warn 
the user when they mangle the "main" entry point before; we didn't warn on

     void main(String[] args)  // no static

or

     static void main()        // no args

or

     static void main(Sling[] args)  // wrong args type


These were all valid Java method declarations, but might not do what the 
author expected, because the launch protocol has always been nearly 
extralinguistic.  This JEP doesn't really bring the launch protocol that 
much more into the spotlight.

I do think it is fair game for an IDE inspection, though (as if Tagir 
doesn't already have one.)


On 9/18/2023 12:47 PM, Alex Buckley wrote:
> Traditionally, `void main()` was not an entrypoint, but under JEP 445, 
> it is. This is a source compatible change, but not a behaviorally 
> compatible change. For example, consider a slightly modified example:
>
> class MissingWarningsSuper {
>     public static void main(String[] args) {}
> }
>
> class MissingWarnings extends MissingWarningsSuper {
>     void main() {}
> }
>
> Traditionally, `java MissingWarnings` would have run the inherited 
> static main. Under JEP 445, `java MissingWarnings` will run the 
> declared instance main. (JLS 12.1.4 has a nice discussion of this 
> policy.)
>
> I think a preview warning is appropriate in order to highlight the new 
> role enjoyed by various methods called "main".
>
> Alex
>
> On 9/18/2023 7:17 AM, Tagir Valeev wrote:
>> But this code is a perfectly valid code in non-preview Java version
>> (unless you try to launch this class, but you don't know this during
>> the compilation). I don't think there should be a warning.
>>
>> With best regards,
>> Tagir Valeev.
>>
>> On Mon, Sep 18, 2023 at 9:37 AM Remi Forax <forax at univ-mlv.fr> wrote:
>>>
>>> Hello,
>>> I believe this code should emit a warning when compiled given it 
>>> uses an instance main method
>>>
>>> class MissingWarnings {
>>>    void main() {
>>>    }
>>> }
>>>
>>> But the command
>>>    javac --enable-preview --source 21 MissingWarnings.java
>>> does not print a warning.
>>>
>>> regards,
>>> Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230918/e9f9a619/attachment.htm>


More information about the amber-dev mailing list