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();
The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on whether it is applied to a class, a variable, or a method.
The final is a modifier in Java, which can be applied to a variable, a method, or a class. Though, you can have a final block. Also, you can use the final modifier with local variables, class variables, as well as with instance variables.
If you prefix a class definition with final , you prevent anyone from subclassing that class. Value types don't support inheritance so it doesn't make sense to mark a struct or enum as final . The final keyword communicates to the compiler that the class cannot and should not be subclassed.
You can declare a variable in any scope to be final. . The value of a final variable cannot change after it has been initialized. Such variables are similar to constants in other programming languages.
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:
public MyConstructor() {
final int localIntVar = 3; // this must be final
myJButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// because you use the local variable inside of an anon inner class
// not because this is a Swing application
System.out.println("localIntVar is " + localIntVar);
}
});
}
and a Non-Swing example:
public void myMethod() {
final String foo = "Hello"; // again it must be final if used in
// an anon inner class
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(foo);
try {
Thread.sleep(1000);
} catch (Exception e) {}
}
}
}).start();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With