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 7635701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:29:33+00:00 2026-05-31T07:29:33+00:00

I am updating various swing components using the event dispatching thread. However when I

  • 0

I am updating various swing components using the event dispatching thread. However when I do something like the following I get an error –

public Foo(User user) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            getNameTextField.setText(user.getName());
        }
     });
}

The error is – “local variable user is accessed from within inner class; needs to be declared final”

What is the standard solution to this…just put final before each function parameter?

Now obviously I dont want to declare my actual user object final as I might need to modify it in future. But is it ok to just set final on the user object in the function parameter?

But what am I actually doing when I set that to final as that is a reference to the ‘main’ user object in my program. So the same object is declared final in one place, yet not final in another. Does declaring it final simply mean the reference to it is final, however the user object itself can be modified freely?

  • 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-31T07:29:34+00:00Added an answer on May 31, 2026 at 7:29 am

    The solution is to make the function arguments final as you suggest.

    The final modifier means that the reference cannot be reassigned after the first assignment. It confers no guarantees on the referred object.

    btw, This is an artefact of the way anonymous inner classes were ‘bodged’ into the language. The compiler creates hidden fields for each of the arguments, if you decompile your code above you can see what’s going on…

    In the decompilation below you see that a new class Foo$1 is created (foo method #0) and at line #6 it is passed the User arg in it’s constructor. You’ll see similar shenanigans going on in the decompilation of Foo$1.class sigh

    Compiled from “Foo.java”

    public class Foo extends java.lang.Object{
    
    protected javax.swing.JTextField nameTextField;
    
    public Foo();
      Code:
       0:   aload_0
       1:   invokespecial   #10; //Method java/lang/Object."<init>":()V
       4:   return
    
    public void foo(User);
      Code:
       0:   new     #18; //class Foo$1
       3:   dup
       4:   aload_0
       5:   aload_1
       6:   invokespecial   #20; //Method Foo$1."<init>":(LFoo;LUser;)V
       9:   invokestatic    #23; //Method java/awt/EventQueue.invokeLater:        (Ljava/lang/Runnable;)V
       12:  return
    
    }
    

    Compiled from “Foo.java”

    class Foo$1 extends java.lang.Object implements java.lang.Runnable{
    final Foo this$0;
    
    private final User val$user;
    
    Foo$1(Foo, User);
      Code:
       0:   aload_0
       1:   aload_1
       2:   putfield    #14; //Field this$0:LFoo;
       5:   aload_0
       6:   aload_2
       7:   putfield    #16; //Field val$user:LUser;
       10:  aload_0
       11:  invokespecial   #18; //Method java/lang/Object."<init>":()V
       14:  return
    
    public void run();
      Code:
       0:   aload_0
       1:   getfield    #14; //Field this$0:LFoo;
       4:   getfield    #26; //Field Foo.nameTextField:Ljavax/swing/JTextField;
       7:   aload_0
       8:   getfield    #16; //Field val$user:LUser;
       11:  invokeinterface #32,  1; //InterfaceMethod User.getName:()Ljava/lang/String;
       16:  invokevirtual   #38; //Method javax/swing/JTextField.setText:(Ljava/lang/String;)V
       19:  return
    
    }
    

    Here’s my Foo.java (your’s didn’t compile…)

    import javax.swing.JTextField;
    
    public class Foo {
    
        protected JTextField nameTextField;
    
        public void foo(final User user) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    nameTextField.setText(user.getName());
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing some CRUD screens for retrieving/updating various items using .NET 3.5 and aspx
When updating a DataTable with 1850-ish new rows to a FbDataAdapter I get a
After updating to Parsec 3.1 from 2.x, code using many1, such as word =
After updating to XCode 4.3, all my archives built using XCode 4.2 are gone.
For updating in my .properties file I am using Apache Commons Configuration's PropertiesConfiguration .
I have a Java Swing UI that isn't updating/repainting as I thought it should.
I use the django admin for updating various data on the MySQL database. I
I'm writing a Django app that performs various functions, including inserting, or updating new
I'm working on updating the GUI of a native (MFC) application using .NET and
I'm building a system for updating large amounts of data through various CSV feeds.

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.