Missing warning when using an instance main method

Alex Buckley alex.buckley at oracle.com
Mon Sep 18 16:47:16 UTC 2023


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


More information about the amber-dev mailing list