<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body><div style="font-family: sans-serif;"><div class="markdown" style="white-space: normal;">
<p dir="auto">On 28 Sep 2022, at 13:49, Remi Forax wrote:</p>
</div><div class="plaintext" style="white-space: normal;"><blockquote style="margin: 0 0 5px; padding-left: 5px; border-left: 2px solid #777777; color: #777777;"><p dir="auto">- 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.
<br>
Every syntax that make that distinction murkier is a bad idea.
<br>
So perhaps what we want is a classless container of methods, not a classless class.</p>
</blockquote></div>
<div class="markdown" style="white-space: normal;">
<p dir="auto">Hmmm…  That would be an interface.  I’ll pull on that thread a little:</p>
<p dir="auto">An interface has no non-static fields and (bonus) its static fields are always constant.  So you can teach interface <em>as a container</em> without getting into mutability.</p>
<p dir="auto">Methods would have to be implicitly decorated with <code style="margin: 0; padding: 0 0.4em; border-radius: 3px; background-color: #F7F7F7;">default</code> in an anonymous <em>interface</em>.</p>
<p dir="auto">The execution of an instance-main anonymous interface would look almost <em>exactly</em> like that for a class:</p>
<p dir="auto"><code style="margin: 0; padding: 0 0.4em; border-radius: 3px; background-color: #F7F7F7;">public static void main(String[] av) { new <ThisClass>(){}.main(); }</code></p>
<p dir="auto">The only difference is the <code style="margin: 0; padding: 0 0.4em; border-radius: 3px; background-color: #F7F7F7;">{}</code>.  Abstracts would be forbidden in an anonymous interface:  Every method has a body, just as every field has an initializer.</p>
<p dir="auto">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.</p>
<p dir="auto">Objection:  <em>That’s no interface!</em>  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).</p>
<p dir="auto">Bonus:  If the “instance main” feature is supported <em>only for interface containers</em> 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.</p>

</div></div></body>

</html>