Draft Spec for Unnamed Classes and Instance main Methods (JEP 445)

Tagir Valeev amaembo at gmail.com
Wed May 3 13:55:23 UTC 2023


Hello!

Great job, thank you!

To me, the discussion in "7.3 Compilation Units" section looks
confusing. It's written:

An unnamed top level class compilation unit consists of:
...
- The declarations of the members of the implicitly declared top level
class, at least one of which is a field declaration (8.3) or a method
declaration (8.4).
...
the following is unambiguously an unnamed top level class compilation unit:

import p.*;
int count = 0;
class Test {}

And below:
> It is a compile-time error if this class does not declare a candidate main method (12.1.4).

So the first obvious problem is that the sample above is not a valid
Java program, as it lacks the main method. And if it's not a valid
Java program, it looks like there is no reason to classify whether
this particular sample is an unnamed compilation unit, or named one.
It's up to the tool which compilation error to issue here: lack of
main method in unnamed compilation unit or misplaced variable
declaration in ordinary compilation unit. I'm inclined to think that
the latter would be less confusing, but apparently this is up to the
tool implementation. Smart tools may suggest both fixes: either move
the `count` declaration into class or declare the `main` method.

As a candidate main method must present and it cannot be inherited
from super-class/super-interface (due to the absence of them), it
looks like we can simplify this part:

An unnamed top level class compilation unit consists of:
- zero or more import declarations that allow classes and interface
from other packages, and static members of classes and interfaces, to
be referred to using their simple names.
- The declarations of the members of the implicitly declared top level
class, at least one of which is a candidate main method (12.1.4).

No field mention is necessary at all. And the sample should be updated
accordingly, like

import p.*;
class Test {}
void main() {}

What do you think?

With best regards,
Tagir Valeev.

On Wed, May 3, 2023 at 12:17 AM Gavin Bierman <gavin.bierman at oracle.com> wrote:
>
> Dear experts:
>
> The first draft of the spec change document for the feature Unnamed classes and instance main methods [1] (the "on-ramp" feature) is now available at:
>
> https://cr.openjdk.org/~gbierman/jep445/latest/
>
> Please give us your feedback (either on this list or directly to me).
>
> Thanks,
> Gavin
>
> [1] Unnamed classes and instance main methods: https://openjdk.org/jeps/445


More information about the amber-dev mailing list