wood burning stoves
The moose likes Java in General and the fly likes Using final for anonymous inner classes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using final for anonymous inner classes." Watch "Using final for anonymous inner classes." New topic

Using final for anonymous inner classes.

Adam Till
Ranch Hand

Joined: Oct 11, 2002
Posts: 41
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?

Adam Till<br />SCJP SCJD SCWCD
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

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.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
I agree. Here's the link: http://aspose.com/file-tools
subject: Using final for anonymous inner classes.
It's not a secret anymore!