Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing and final modifier

Tags:

java

swing

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();
like image 922
Cemre Mengü Avatar asked Jan 05 '12 15:01

Cemre Mengü


People also ask

What is final modifier?

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.

Where can the final modifier be applied?

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.

What happens if I write final keyword just prefix of any method?

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.

What is final variable?

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.


1 Answers

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:

  • Promote the variable to a class field, giving it class scope.
  • Have the anonymous inner class call a method of the outer class. But then, the variable will still need to have class scope.

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

like image 149
Hovercraft Full Of Eels Avatar answered Oct 04 '22 22:10

Hovercraft Full Of Eels