Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anonymous inner class

 
prateek narang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic