Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:59:52+00:00 2026-05-27T21:59:52+00:00

Why does SWING always force me to mark some particular objects as final ?

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:59:53+00:00Added an answer on May 27, 2026 at 9:59 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the text in Swing components have a default font? In particular, what about
Do you know what is the name of this layout for Java/Swing? Does the
How does the event creation and handling work in Java Swing?
Does Google force employees who have offers from Facebook to leave immediately?
What is the importance of JTable model.Does all the swing components has model associated
How does one go about adding JButton s among various other Swing GUI components
I'm doing some Swing GUI work with Java, and I think my question is
I am building a swing application. At some point, I have to start an
Swing applications always start maximised on one of my computers, but not on others.
I always coded console applications and learned some basic UML/patterns skills, using C++. Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.