File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final automatic variables" Watch "final automatic variables" New topic

final automatic variables

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 808

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?

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:

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

Joined: Aug 03, 2008
Posts: 9462

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:
subject: final automatic variables
It's not a secret anymore!