New candidate JEP: 445: Flexible Main Methods and Anonymous Main Classes (Preview)

Ron Pressler ron.pressler at oracle.com
Tue Apr 11 21:49:18 UTC 2023



On 11 Apr 2023, at 22:26, Hunor Szegi <hunor.szegi at gmail.com<mailto:hunor.szegi at gmail.com>> wrote:

Ok, I phrased it incorrectly. I see that technically it isn't static. My concern is that the "main" method is usually static (until now it is always that), but now you will be able to create a main method without the "static" keyword. So for a beginner, without understanding fully the difference between static and non-static (which isn't easy!) it seems that the main became implicitly static. At least it behaves very similarly, and it will work with or without the static keyword. So it can lead to confusion. The class instantiation is hidden, so syntactically it seems the main method became static automatically.

1. Flexible main methods don’t only work in anonymous main classes but all classes, so there’s never a need to change anything.

2. Anyone who knows what main “usually” used to be can continue using that flavour if they prefer.

3. The signature of main methods is, and has always been, rather arbitrary. There’s nothing fundamental that says it should be static or an instance method. Instance main methods don’t work “as if they were static”; rather, they work like instance main methods should work, and would have worked if they had existed in Java since day one.

4. Indeed, a class should have one entry point, and linters/IDEs will warn if there are multiple ones. A corpus search revealed that the portion of existing classes with more than one candidate main method is negligible, so we don’t think it would be a problem in practice. The ordering is needed for backward compatibility.

Having said all that, if, after trying the feature you find that you run into problems, please report them to this mailing list. One thing we could consider changing is to emit an error on multiple candidates as long as none of them is the current signature.

— Ron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230411/68ac74e1/attachment.htm>


More information about the amber-dev mailing list