I read this on one of the websites. I am not able to figure it out "An inner class can access only those local variables and parameters, that are declared as final." Can some one explain with some example. Best regards Gunjan
Regards<BR>Gunjan
Tony Alicea
Desperado
Sheriff
Joined: Jan 30, 2000
Posts: 3219
posted
0
They mean inner LOCAL classes, not ALL inner class types. Local classes are classes defined inside methods. For a local variable of the method to be used inside a local class defined inside that method, it has to be final: <PRE> public class Test { public void oneMethod(final int x) { int localInt=0; final int anotherInt=0; class LocalClass{ // int ii = localInt; < - CANNOT USE NON-FINAL VARIABLE int jj = anotherInt; int kk = x; } } } </PRE>
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
...and just to clarify, the term "local classes" includes anonymous classes, but excludes member classes and top-level nested classes.
"I'm not back." - Bill Harding, Twister
Thiru Thangavelu
Ranch Hand
Joined: Aug 29, 2001
Posts: 219
posted
0
Top level nested class means static member class only. isn't?
Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
john sham
Greenhorn
Joined: Jan 30, 2002
Posts: 6
posted
0
Originally posted by Thiru Thangavelu: Top level nested class means static member class only. isn't?
Yes, Top level nested class means static class members.
Francisco A Guimaraes
Ranch Hand
Joined: Mar 20, 2002
Posts: 182
posted
0
just a doubt in semantics: when you say "member class", is that a more general term or is it specific to non-static, non-local inner classes? Francisco
Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Igor Romanov
Greenhorn
Joined: Nov 24, 2001
Posts: 23
posted
0
That is because if the inner class defined in the method's scope, than an object of that class can possibly outlives the method invocation.