<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>"David Alayachew" <davidalayachew@gmail.com><br><b>To: </b>"compiler-dev" <compiler-dev@openjdk.org><br><b>Sent: </b>Saturday, September 30, 2023 5:22:09 AM<br><b>Subject: </b>I don't understand the "this-escape" warning under this context<br></blockquote></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 dir="ltr"><div class="gmail_default" style="font-family:monospace">Hello Compiler Dev Team,</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">I have the following code example.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">```java</div><div class="gmail_default" style="font-family:monospace"><br>package Paint;<br><br>import javax.swing.*;<br><br>public class GUI<br>{<br><br> private final JFrame frame;<br><br> public GUI()<br> {<br> <br> this.frame = new JFrame();<br> <br> this.frame.add(this.createBottomPanel());<br> <br> }<br><br> private final JPanel createBottomPanel()<br> {<br> <br> final JButton save = new JButton();<br> <br> save<br> .addActionListener<br> (<br> actionEvent -><br> {<br> <br> this.toString();<br> <br> }<br> <br> )<br> ;<br> <br> return null;<br> <br> }<br><br>}<br></div><div class="gmail_default" style="font-family:monospace">```</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">This is the compile command that I used.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">```</div><div class="gmail_default" style="font-family:monospace">javac -Xlint:all
GUI.java</div><div class="gmail_default" style="font-family:monospace">```</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">When I compile, I get the following warning.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">```</div><div class="gmail_default" style="font-family:monospace">GUI.java:16: warning: [this-escape] possible 'this' escape before subclass is fully initialized<br> this.frame.add(this.createBottomPanel());<br> ^<br>GUI.java:28: warning: [this-escape] previous possible 'this' escape happens here via invocation<br> actionEvent -><br> ^<br>2 warnings</div><div class="gmail_default" style="font-family:monospace">```</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">Can someone help me understand the what, why, and how of this warning? I don't understand it at all.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">I am pretty sure I understand the basic premise of "this-escape" -- a not-fully-initialized object (this) can "escape" from its constructor before completing initialization, usually causing bugs and security vulnerabilities.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">A good example of this is a constructor calling an overridable method. If a subclass overrides that method, it may expose the state or perform behaviour that it is not prepared to perform at that time, amongst other things.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">But I struggle to follow that logic for my example. Could someone walk me through this ?</div></div></blockquote><div><br></div><div>The first error seems to be a false positive because as you said the method createBottomPanel() is private (BTW, all private methods can not be overriden so declaring a private method final is not really useful).<br data-mce-bogus="1"></div><div>The second error is due to the fact that the lambda can be called before the end of the constructor, the compiler is not smart enough to know if this is the case or not.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>The usual workaround is to create the GUI inside static methods, the constructor being a static factory named by example createGUI() and createBottomPanel() being declared static.<br data-mce-bogus="1"></div><div>If you need objects, pass them as parameter of the static methods, so they are known to be fully initialized.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Apart for the bugs with subclassing, the problem of publication in a concurrency context, having a constructor that escapes "this" also prevents a class to be a value class.<br data-mce-bogus="1"></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 dir="ltr"><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">Thank you for your time and help!</div><div class="gmail_default" style="font-family:monospace">David Alayachew</div></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>