Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using final for anonymous inner classes.

 
Adam Till
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have a method that creates a JFrame, within this is a button that has an anonymous inner class that disposes of the frame. The JFrame has to be final so it can be referenced in the anonymous inner class, what are the consequences of having a JFrame final?
I suspect not much as it only makes the reference to the JFrame constant. Is this correct?
Thanks
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making the JFrame in the top-level final puts the JFame in-line with the compiled class, where any inner class is guaranteed it can complete a reference. An anonymous inner class would not be able to refer to an element outside its top-level class, so insisting on the final scope avoids trouble.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic