aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class and Final Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Inner Class and Final Variables" Watch "Inner Class and Final Variables" New topic
Author

Inner Class and Final Variables

gunjan
Ranch Hand

Joined: Jan 28, 2000
Posts: 33
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
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
...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
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
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
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
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.


cheers,<br />Igor Romanov
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class and Final Variables
 
Similar Threads
the Quession about static of inner class
Help with Inner Classes
Inner classes
regarding inner class
inner class access?