• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method local class- final vairable

 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21431
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code

Now after compilation the code would look as (I have removed the commented lines for simplicity...)
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since you asked for so hari you can read my blog...the link is in my signature....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic