It's not a secret anymore!*
The moose likes Java in General and the fly likes anonymous inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "anonymous inner class" Watch "anonymous inner class" New topic
Author

anonymous inner class

prateek narang
Greenhorn

Joined: Apr 04, 2001
Posts: 19
hi group,
in the filter() method of the code below the argument passed is final, this argument is used in the anonymous class written inside that method. My question is that why we have to declare this argument as final?

regds..
--prateek--
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Prateek,
Quite simply, because the class will out live the method. Therefore, if it was not final the reference to it would be gone and the anonymous class would be using a undefined reference variable. Not very good for a stable environment.
Regards,
Manfred.
prateek narang
Greenhorn

Joined: Apr 04, 2001
Posts: 19
But Manfred this means that final object lives after the scope of the method has been finished. I'm not sure about the scope of the final variables. can anybody explain a bit about it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: anonymous inner class
 
Similar Threads
Get files based on Pattern Matching Algorithm
The method list(FilenameFilter) in the type File is not applicable for the arguments (FilenameFilter
Why doesn't named classes classes doesn't insist on final parameter variable?
FilenameFilter not working???
how to find example (quickly)?