Unnamed Classes with Initializers
Wesley Egberto
wesleyegberto at gmail.com
Tue Jul 4 00:54:27 UTC 2023
Hi all
First off, please let me know if I have sent this to the wrong group.
I'm exploring the JEPs in JDK 21, with the JEP 445 Unnamed Classes and
Instance Main Methods I'm getting a compiler error when using instance and
static initializers.
I would like to know if I misunderstood the JEP or if I'm missing something.
The JEP says:
"An unnamed class is almost exactly like an explicitly declared class. Its
> members can have the same modifiers (e.g., private and static) and the
> modifiers have the same defaults (e.g., package access and instance
> membership). *The class can have static initializers as well as instance
> initializers.* One key difference is that while an unnamed class has a
> default zero-parameter constructor, it can have no other constructor."
When I try to compile the following code:
static {
System.out.println("static initializer");
}
{
System.out.println("instance initializer");
}
void main() {
System.out.println("instance main method");
}
I got the following error:
UnnamedClassWithInitializers.java:1: error: class, interface, enum, or
record expected
static {
^
UnnamedClassWithInitializers.java:3: error: class, interface, enum, or
record expected
}
^
UnnamedClassWithInitializers.java:7: error: class, interface, enum, or
record expected
}
^
UnnamedClassWithInitializers.java:11: error: class, interface, enum, or
record expected
}
^
4 errors
error: compilation failed
Thank you for your time and work!
Wesley Egberto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230703/de4fd650/attachment.htm>
More information about the amber-dev
mailing list