aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method local class- final vairable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method local class- final vairable" Watch "method local class- final vairable" New topic
Author

method local class- final vairable

hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Anybody could explain me why a inner class object can use the local variables of the method the inner class is in when these ones are declared final.

It's confused to get it when you think that the scope of a local variable is limited to the method the variable is declared in. so, any special reason??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Well, technically, it is not using the local variable. If the local variable is final, the inner class can assume that the variable will not change, and hence, make a copy of it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Here's the code

Now after compilation the code would look as (I have removed the commented lines for simplicity...)


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Thanks Ankit..but i wonder where from do you get all these details(i have read lot of your replys in the forum).. if you could give any resource i think that could help scjp aspirants a lot...
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Thanks Ankit..but i wonder where from do you get all these details(i have read lot of your replys in the forum).. if you could give any resource i think that could help scjp aspirants a lot...
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

The class cannot use the variables that are not Final.
Because,

The scope of method specific variable is only inside the method. The variable dies once the control exits the block.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

since you asked for so hari you can read my blog...the link is in my signature....
 
jQuery in Action, 2nd edition
 
subject: method local class- final vairable