| 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: 16681
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
since you asked for so hari you can read my blog...the link is in my signature....
|
 |
 |
|
|
subject: method local class- final vairable
|
|
|