Paving the on-ramp

Jim Laskey james.laskey at oracle.com
Thu Sep 29 16:47:17 UTC 2022


    // Hello.java
    public class Hello {
     // tons of logic
    }

    void main() {
    }

and

    void main() {
    }

    // Hello.java
    public class Hello {
     // tons of logic
    }

Are equivalent, as though the file content is wrapped in an outer class.

public class {$name} {}
    // Hello.java
    public class Hello {
     // tons of logic
    }

    void main() {
    }
}

The trigger for an unnamed class is a method or field defined at the top level. So the order doesn’t matter.

{$name} is derived from the source file name and must be a valid identifier.

When running the source launcher, the class name doesn't matter (we could allow only with the source launcher).
When compiling with javac we have to stuff the class somewhere and using a name derived from the source makes sense.
So if the source is Hello.java you can access the class Hello.Hello from an external reference.

Cheers,

— Jim










On Sep 29, 2022, at 4:07 AM, Tagir Valeev <amaembo at gmail.com<mailto:amaembo at gmail.com>> wrote:

Hello!

Very interesting writing, thanks! A couple of notes from me:

## Unnamed classes
...
Such source files can still have fields, methods, and even nested classes, so
that as a program evolves from a few statements to needing some ancillary state
or helper methods, these can be factored out of the `main` method while still

I wonder how we tell apart unnamed class syntax and normal class
syntax. E.g., consider the source file:

// Hello.java
public class Hello {
 // tons of logic
}

void main() {
}

Will it be considered as a correct Java file, having Hello class as a
nested class of top-level unnamed class?
If yes, then, adding a main method after the class declaration, I
change the class semantics, making it an inner class.
This looks like action at a distance and may cause confusion. E.g., I
just wrote a main() method outside of Hello class instead of inside,
and boom,
now Hello is not resolvable from other classes, for no apparent reason.

I assume that the main() method is required for an unnamed class, and
if there are only other top-level declarations,
then it should be a compilation error, right?

## Predefined static imports
```
void main() {
   println("Hello World");
}
```

I wonder how it will play with existing static star imports. We
already saw problems when updated to Java 9 or Java 14 that
star-imported class named Module or Record becomes unresolvable. If
existing code already imports static method named println from
somewhere, will this code become invalid?

With best regards,
Tagir Valeev.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20220929/c4dc4f12/attachment-0001.htm>


More information about the amber-spec-experts mailing list