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 Regarding Method-local inner classes 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 "Regarding Method-local inner classes" Watch "Regarding Method-local inner classes" New topic

Regarding Method-local inner classes

Prasad Maddipatla

Joined: Apr 20, 2007
Posts: 24
Hi Friends,

We have a rule that " A method-local inner class cannot access the local variables in the method in which it is declared" but when we make local variables as final we can use them in the method local inner class. Can any body explain what the real difference when we make local varables as final so that they can be used in method-local inner classes.
marc weber

Joined: Aug 31, 2004
Posts: 11343

When a method exits, its local variables are destroyed. But an instance of a local class might continue to exist after the method exits, and if it tried to access a local variable at that point, there would be a problem. On the other hand, if a local variable is final, then its value can be copied into the instance of the local class, and the instance can use it essentially as a constant.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
subject: Regarding Method-local inner classes
It's not a secret anymore!