On 28 Sep 2022, at 13:49, Remi Forax wrote:

- We should not be able to declare fields inside a classless class, students strugle at the beginning to make the difference between a field and a local variable.
Every syntax that make that distinction murkier is a bad idea.
So perhaps what we want is a classless container of methods, not a classless class.

Hmmm… That would be an interface. I’ll pull on that thread a little:

An interface has no non-static fields and (bonus) its static fields are always constant. So you can teach interface *as a container* without getting into mutability.

Methods would have to be implicitly decorated with `default` in an anonymous *interface*.

The execution of an instance-main anonymous interface would look almost *exactly* like that for a class:

`public static void main(String[] av) { new <ThisClass>(){}.main(); }`

The only difference is the `{}`. Abstracts would be forbidden in an anonymous interface: Every method has a body, just as every field has an initializer.

Bonus: No instance initializers, since it’s an interface. (No constructors either.) So the headaches about initialization-related syntaxes go away without additional special pleading.

Objection: *That’s no interface!* Well, true. Except it is an interface to the system, being a launch point. (Is that just a bad pun?) Also, folks use interfaces today as an idiom for a lightweight container of Java code (at least, I do that).

Bonus: If the “instance main” feature is supported *only for interface containers* then some issues of accidentally creating a main (in existing code) go away, simply because the attack surface (for accidents) gets smaller. Yes, that’s a yucky bonus.

Reply via email to