Why does SWING always force me to mark some particular objects as final ? Since this sometimes makes things a bit difficult, Is there a way to avoid this ?
(INCOMPLETE EXAMPLE) where it forces me to mark the IExchangeSource variable as final:
public class MainFrame {
private final JTextArea textArea = new JTextArea();
public static void main(final IExchangeSource s) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainFrame(s);
}
});
}
public MainFrame(final IExchangeSource s) {
//build gui
s.update();
This has nothing to do with Swing, nothing at all. You should show your code that has an example of this, but likely you are using an inner class, possibly an anonymous inner class, and if you use these and try to use variables inside the inner class that are local to an enclosing method (or other block such as a constructor), then you are requried to make these variables final or promote them to class fields. Again this is a Java requirement, not a Swing requirement.
A Swing example:
and a Non-Swing example:
There are several tricks to avoid this:
Edit 2
Anthony Accioly posted an answer with a great link, but then for unknown reasons deleted his answer. I’d like to post his link here, but would love to see him re-open his answer.
Cannot refer to a non-final variable inside an inner class defined in a different method