<div dir="ltr"><div dir="ltr">Hi Ella,<br></div><div><br></div><div>Thanks for the report.</div><div><br></div><div>First, we can dispense with Case 3 - that program doesn't compile under any JDK version ("variable x might not have been initialized"), because initializer blocks run immediately after super(), which in this case is (implicitly) located at the beginning of the constructor and therefore prior to the assignment of x.<br></div><div><br></div><div>Having said that, now take a look at Case 2: it has the same problem, but in a slightly different form.</div><div><br></div><div>You can see this by trying to compile this version of Case 2 with the first constructor removed:<br></div><div><br></div><div><p class="MsoNormal" style="background:white;margin-left:40px"><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">class
</span><span style="font-size:10pt;font-family:"Courier New";color:black">Q
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">{<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">final int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">y</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
    { </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">y
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">=
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">; }<br>   
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">public
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,98,122)">Q</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">() {}<br>
}</span></p><p class="MsoNormal" style="background:white"><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)"><br></span></p><p class="MsoNormal" style="background:white"><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)"></span>You get the same error "variable x might not have been initialized".<br></p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white">When you add back the first constructor to get your original Case 2, you still haven't fixed that problem.</p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white">Then the compiler reports this:</p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white;margin-left:40px"><span style="font-family:monospace">Q.java:7: error: variable x might already have been assigned<br>        this();<br>            ^<br>Q.java:4: error: variable x might not have been initialized<br>    { y = x; }<br>          ^</span><br></p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white">The error reported on line 7 is slightly confusing, but it's really just a side effect of the error on line 4 that was already present.</p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white">In other words, clearly someone could invoke the second constructor Q() and there would be nothing that initializes field x, so the program is not valid.</p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white">On the other hand, the compiler assumes that the other constructor is going to properly initialize all final fields when it sees this(), and therefore it deduces that a duplicate assignment must be occurring. This assumption is wrong, but only because constructor Q() is invalid.<br></p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white">Here's a version that does compile. Note that it fails if you uncomment the commented-out line.<br></p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white;margin-left:40px"><span style="font-family:monospace">class Q {<br>    final int x;<br>    int y;<br>    { y = x; }<br>    public Q(int a) {<br>        //x = a;    // "variable x might already have been assigned"</span><br><span style="font-family:monospace">        this();<br>    }<br>    public Q() {<br>        x = 3;<br>        super();<br>    }<br>}</span><br></p><p class="MsoNormal" style="background:white"><br></p><p class="MsoNormal" style="background:white">-Archie<br></p></div><div><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, May 20, 2024 at 4:13 PM Ella Ananeva <<a href="mailto:ella.ananeva@oracle.com">ella.ananeva@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg1797344161375976331">





<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_7398756463249314942WordSection1">
<p class="MsoNormal">Hi,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I found a discrepancy in the behavior of the JEP 482 compiler regarding definite assignment of a final non-static blank field in a prologue of a constructor:<u></u><u></u></p>
<table border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;border:medium">
<tbody>
<tr>
<td width="208" valign="top" style="width:155.8pt;border:1pt solid windowtext;padding:0in 5.4pt">
<p class="MsoNormal">Case 1<u></u><u></u></p>
</td>
<td width="208" valign="top" style="width:155.85pt;border-width:1pt 1pt 1pt medium;border-style:solid solid solid none;border-color:windowtext windowtext windowtext currentcolor;padding:0in 5.4pt">
<p class="MsoNormal">Case 2<u></u><u></u></p>
</td>
<td width="208" valign="top" style="width:155.85pt;border-width:1pt 1pt 1pt medium;border-style:solid solid solid none;border-color:windowtext windowtext windowtext currentcolor;padding:0in 5.4pt">
<p class="MsoNormal">Case 3<u></u><u></u></p>
</td>
</tr>
<tr>
<td width="208" valign="top" style="width:155.8pt;border-width:medium 1pt 1pt;border-style:none solid solid;border-color:currentcolor windowtext windowtext;padding:0in 5.4pt">
<p class="MsoNormal" style="background:white"><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">class
</span><span style="font-size:10pt;font-family:"Courier New";color:black">Q
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">{<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">final int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">y</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
    { </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">y
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">=
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">; }<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">public
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,98,122)">Q</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">(</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">int
</span><span style="font-size:10pt;font-family:"Courier New";color:black">a</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">) {<br>
        </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">=
</span><span style="font-size:10pt;font-family:"Courier New";color:black">a</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
        </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179);background:yellow">super</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8);background:yellow">();</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">   
<u></u><u></u></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">    }<br>
}<u></u><u></u></span></p>
</td>
<td width="208" valign="top" style="width:155.85pt;border-width:medium 1pt 1pt medium;border-style:none solid solid none;border-color:currentcolor windowtext windowtext currentcolor;padding:0in 5.4pt">
<p class="MsoNormal" style="background:white"><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">class
</span><span style="font-size:10pt;font-family:"Courier New";color:black">Q
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">{<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">final int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">y</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
    { </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">y
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">=
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">; }<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">public
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,98,122)">Q</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">(</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">int
</span><span style="font-size:10pt;font-family:"Courier New";color:black">a</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">) {<br>
        </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">=
</span><span style="font-size:10pt;font-family:"Courier New";color:black">a</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
        </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179);background:yellow">this</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8);background:yellow">();</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)"><br>
    }<u></u><u></u></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">   
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">public
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,98,122)">Q</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">() {}<br>
}<u></u><u></u></span></p>
</td>
<td width="208" valign="top" style="width:155.85pt;border-width:medium 1pt 1pt medium;border-style:none solid solid none;border-color:currentcolor windowtext windowtext currentcolor;padding:0in 5.4pt">
<p class="MsoNormal" style="background:white"><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">class
</span><span style="font-size:10pt;font-family:"Courier New";color:black">Q
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">{<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">final int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">y</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
    { </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">y
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">=
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">; }<br>
    </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">public
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,98,122)">Q</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">(</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">int
</span><span style="font-size:10pt;font-family:"Courier New";color:black">a</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">) {<br>
        </span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">x
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">=
</span><span style="font-size:10pt;font-family:"Courier New";color:black">a</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;
<br>
    }<br>
}<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</td>
</tr>
<tr>
<td width="208" valign="top" style="width:155.8pt;border-width:medium 1pt 1pt;border-style:none solid solid;border-color:currentcolor windowtext windowtext;padding:0in 5.4pt">
<p class="MsoNormal">Compilation succeeds<span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)"><u></u><u></u></span></p>
</td>
<td width="208" valign="top" style="width:155.85pt;border-width:medium 1pt 1pt medium;border-style:none solid solid none;border-color:currentcolor windowtext windowtext currentcolor;padding:0in 5.4pt">
<p class="MsoNormal" style="background:white"><span style="color:black">Compilation fails</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)"><u></u><u></u></span></p>
</td>
<td width="208" valign="top" style="width:155.85pt;border-width:medium 1pt 1pt medium;border-style:none solid solid none;border-color:currentcolor windowtext windowtext currentcolor;padding:0in 5.4pt">
<p class="MsoNormal" style="background:white"><span style="color:black">Compilation fails</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)"><u></u><u></u></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><u></u> </p></div></div></div></blockquote></div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div>