Adding an 'init' keyword for initializers in Java
Chen Liang
chen.l.liang at oracle.com
Mon Mar 24 03:18:00 UTC 2025
Hi, this mailing list is for general governance of the jdk project of OpenJDK instead for specific proposals for specific areas.
>From a Java Language-specific perspective, it seems you are implying to make an instance initializer (JLS 8.6<https://docs.oracle.com/javase/specs/jls/se24/html/jls-8.html#jls-8.6>) an error. This seems unrealistic as the Java programming language has not yet removed any language feature.
In addition, I don't think this problem is valid: Java programmers know that methods are declared with a return type, a name, and a parameter list. (See JLS 8.4, 9.4) There is no such concept as "a method without a name." I think your better bet is to adopt the habit of writing a method declaration before writing a method body and to turn on IDE inspections (if you have an IDE) to make instance initializers a warning.
________________________________
From: jdk-dev <jdk-dev-retn at openjdk.org> on behalf of DLOzTOR <zafabar1 at gmail.com>
Sent: Sunday, March 23, 2025 3:10 PM
To: jdk-dev at openjdk.org <jdk-dev at openjdk.org>
Subject: Adding an 'init' keyword for initializers in Java
I encountered a problem when I mistakenly wrote a method without a name and it was perceived as an initializer, to avoid such cases I want to suggest adding a keyword for declaring initializers.
Something like this:
class Example {
init {}
static init {}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20250324/d66d3330/attachment-0001.htm>
More information about the jdk-dev
mailing list