Adding an 'init' keyword for initializers in Java

Brian Goetz brian.goetz at oracle.com
Mon Mar 24 15:16:45 UTC 2025


Chen is correct that this is not the right list for discussion.

But, for the record: this suggestion (which has been made before) falls 
into the category of "fighting the previous war."  While the syntax that 
you propose is probably better than what was chosen for this originally, 
"fixing" this is unlikely to actually make things better.  The mistake 
you made would be no more unlikely, since the old syntax would still be 
accepted, and worse, there'd now be two ways to say the same thing, 
which might (a) confuse new users that there might be a difference 
between the two, or (b) spark value-destroying "style wars" (think: tabs 
vs spaces) for limited real benefit.  (Taking away the old syntax, 
however unclear or error-prone it might be (and it is), would break 
millions of Java programs, and those whose programs we break will not 
thank us for "making the language better" in this way.)

So even if this is a mistake, its one for which fixing it (at the scale 
of the Java ecosystem) would be more expensive than living with it.

On 3/23/2025 4:10 PM, DLOzTOR wrote:
> 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/c7db85d5/attachment.htm>


More information about the jdk-dev mailing list