Hát mert ez a szabály: local inner class láthatja az outer class memberjeit, de nem láthatja a metódus lokális változóit. Azok csak a stack-ban léteznek (a metódus futásának idejéig), de a local inner class túlélheti a metódust: te is visszaadsz egy példányt.<br>
Forrás: <a href="http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060">http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060</a><br><br>Szerintem a final segítségével tudja a VM átlépni ezt a határt: annak értéke nem változhat meg, tehát a belső osztály létrehozásakor annak értéket "beégetheti". Mintha ilyesmit csinálnál (a szintaxis nem feltétlen jó):<br>
<br><div><br></div><div>public class WhyFinal {</div><div><span style="white-space:pre-wrap">        </span>interface Outer {<span style="white-space:pre-wrap"><br> </span>String method();</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><br> private class defaultOuter implements Outer{<br> private final String s;<br> public defaultOuter(String s){<br> this.s = s;<br> }<br> }<br><br><br></div><div>
<span style="white-space:pre-wrap">        </span>public Outer method(String s) {</div><div><span style="white-space:pre-wrap">                </span>class Inner extends defaultOuter {</div><div><span style="white-space:pre-wrap">                        </span>public String method() {</div>
<div><span style="white-space:pre-wrap">                                </span>return s + " wonka";</div><div><span style="white-space:pre-wrap">                        </span>}</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">                </span>return new Inner(s);</div>
<div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>public static void main(String[] args) {</div><div><span style="white-space:pre-wrap">                </span>System.out.println(new WhyFinal().method("bonka").method());</div>
<div><span style="white-space:pre-wrap">        </span>}</div><div>}</div><br><br clear="all"><br>Üdv:<br><b>Vig Balázs<br></b><br><br>
<br><br><div class="gmail_quote">2012/9/11 Peter Verhas <span dir="ltr"><<a href="mailto:peter@verhas.com" target="_blank">peter@verhas.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word">A következő kis mintapéldában:<div><br></div><div><div>public class WhyFinal {</div><div><span style="white-space:pre-wrap">        </span>interface Outer {</div><div><span style="white-space:pre-wrap">                </span>String method();</div>
<div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>public Outer method(final String s) {</div><div><span style="white-space:pre-wrap">                </span>class Inner implements Outer {</div>
<div><span style="white-space:pre-wrap">                        </span>public String method() {</div><div><span style="white-space:pre-wrap">                                </span>return s + " wonka";</div><div><span style="white-space:pre-wrap">                        </span>}</div>
<div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">                </span>return new Inner();</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>public static void main(String[] args) {</div>
<div><span style="white-space:pre-wrap">                </span>System.out.println(new WhyFinal().method("bonka").method());</div><div><span style="white-space:pre-wrap">        </span>}</div><div>}</div><div><br></div><div>miért kell, hogy a 'String s' argumentum final legyen?</div>
<div><br></div><div>
<span style="font-family:Helvetica"><div style="word-wrap:break-word"><span style="text-indent:0px;letter-spacing:normal;font-variant:normal;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><div style="word-wrap:break-word">
<div><span style="color:rgb(136,136,136)">--</span><span style="color:rgb(136,136,136)"><br></span><span style="color:rgb(136,136,136)">Verhás Péter</span><span style="color:rgb(136,136,136)"><br></span><span style="color:rgb(136,136,136)"></span><span style="color:rgb(136,136,136)"><a href="mailto:peter@verhas.com" target="_blank">peter@verhas.com</a></span><span style="color:rgb(136,136,136)"><br>
</span><span style="color:rgb(136,136,136)"><a href="tel:%2B36%2830%299306805" value="+36309306805" target="_blank">+36(30)9306805</a></span><span style="color:rgb(136,136,136)"><br></span><span style="color:rgb(136,136,136)">skype: verhas</span></div>
</div></span><br></div></span><br><br>
</div>
<br></div></div><br>_______________________________________________<br>
Javalist mailing list<br>
<a href="mailto:Javalist@lists.javaforum.hu">Javalist@lists.javaforum.hu</a><br>
<a href="http://lists.javaforum.hu/mailman/listinfo/javalist" target="_blank">http://lists.javaforum.hu/mailman/listinfo/javalist</a><br>
<br></blockquote></div><br>