<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Kenneth Fogel" <kfogel@dawsoncollege.qc.ca><br><b>To: </b>"discuss" <discuss@openjdk.org><br><b>Sent: </b>Saturday, October 26, 2024 5:45:30 PM<br><b>Subject: </b>Make final behave like var<br></blockquote></div><div><style><!--
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Aptos;}
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Aptos",sans-serif;
mso-ligatures:standardcontextual;
mso-fareast-language:EN-US;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Aptos",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:11.0pt;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Just a thought that came up in a reply to a LinkedIn post that contended that ‘final’ was useless because the JIT could figure out what was final and what was not. I disagreed because I felt it would impact the readability
of code. It was then that what may be a silly idea struck me. Could the keyword ‘final’ behave like ‘var’? I do like that you can defer initializing a final variable whereas a var variable must be initialized right away. However, if we want to encourage a
greater use of constant variables (those two words always sound like an oxymoron), and reduce decorations then could final be made to work like var? I shudder to say this, but could we use the term ‘const’ so as not to interfere with existing code. Even have
an ‘sconst’ if we want it static, too.</span></p></div></blockquote><div><br></div><div>const is already a reserved keyword, even if it is not used (now you know :) )<br data-mce-bogus="1"></div><div>https://docs.oracle.com/javase/specs/jls/se23/html/jls-3.html#jls-3.9<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>I really dislike the use of final on local variables for several reasons<br data-mce-bogus="1"></div><div>- it makes the code less readable<br data-mce-bogus="1"></div><div>- final on local variable is not important, there was no 'final' keyword in Java 1.0, it was added in 1.1 to mark local variable that can be captured in an anonymous class.<br data-mce-bogus="1"></div><div> Since 1.8, the compiler computes itself if a local variable is not or not, so the original reason to use final on a local variable has disappear.</div><div>- I can see the point of declaring the parameters final, because debugging a code that changes the value of the parameters is not fun,<br data-mce-bogus="1"></div><div> but, it's a weak argument compared to the readability of the code (IDEs and checkstyle detect those cases).<br data-mce-bogus="1"></div><div>- final on fields is very important, if you want to create an unmodifiable class and forgot to declare your fields final, you have a publication problem (another thread can see the instance half initialized), so for me, final on local variables acts as a stupid distraction, i want to only see the important "final"s.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>And for final on fields, the behavior from the JIT POV depends on the container, inside a record or a hidden class (the captured variables in lambdas), final really mean final, so it's a kind of a mess.<br data-mce-bogus="1"></div><div>As part of valhalla, we want to introduce the concept of strict final fields, a strict final field is a field wich is marked final and initialized before the call to super(), those fields will help the JIT (see https://openjdk.org/jeps/482).<br></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Just thinking out loud. Feel free to use my name in vain.</span></p>
<p class="MsoNormal"><span lang="EN-US">Ken</span></p>
</div></blockquote><div><br></div><div>regards,<br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div></div></div></body></html>