JEP 445

Ron Pressler ron.pressler at oracle.com
Tue Apr 11 08:23:25 UTC 2023



> On 11 Apr 2023, at 05:56, Red IO <redio.development at gmail.com> wrote:
> 
> I was reading the discussions on "anonymous class declarations" which in my understanding simply means that the file scope is treated as class scope of a class named after the file. It was also mentioned that those classes would not be accessible for other source files to reference. 

As far as Java code is concerned, the class is not named after the file; it is anonymous.

> 
> My question is then why should we treat them so special?
> The class name is already strongly coupled to the file name except for package private classes in a file. 
> 
> Why not treat them as regular classes with fields and instances and all?

They can have fields and methods and member classes and even an instance — just like anonymous classes today can. But just like anonymous classes, they can only be instantiated “externally”.

> 
> The only reason I see is to make static variables and the static of main implicit which is a weak argument for treating these classes so special in my opinion. 

There are no implicit statics here. Nothing is changed from the syntax and meaning of anonymous classes.

> 
> If we treat them as regular classes it would even be possible to have generics or inheritance with an optional partial class header syntax like for example:

Nothing is restricted. If you want generics and inheritance, just add a class declaration. The meaning of everything will be preserved.

— Ron



More information about the amber-dev mailing list