File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final automatic variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final automatic variables" Watch "final automatic variables" New topic
Author

final automatic variables

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

If we want to use a local variable inside a local class or anonymous local class - we have to mark it final.
But what does that keyword mean? It means that the reference can not be reassign. But how does it affect the scope of the variable? Why can we use final variables inside a local class?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Final variables are not only prevented from reassignment, they also don't live on the stack like other local variables, but are stored on the heap, which makes them also accessible for inner or anonymous classes.

Also check this thread:

http://www.coderanch.com/t/407046/Java-General-beginner/java/where-final-static-variable-stored


JDBCSupport - An easy to use, light-weight JDBC framework -
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Sebastian Janisch wrote:Final variables are ... stored on the heap, which makes them also accessible for inner or anonymous classes.[/url]


Where final variables are stored is not the reason for them being accessible to local inner and anonymous classes. If you read the thread which you gave link to, Henry has written

Henry Wong wrote:A final local variable is a special case, not because the scope of the local variable is different -- but because since it can't change, the compiler generated code for the inner class to have a copy of the variable. (At instantiation time, when both are in scope)


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final automatic variables
 
Similar Threads
MethodLocal inner class doubt
Method inner class & Final local variables
method variable
Doubt regarding Method Local Inner Class
Method Local Inner classes