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

  • Home
  • SEARCH
  • 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 7929831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:13:04+00:00 2026-06-03T20:13:04+00:00

As you can see in the following code, I am accessing a JLabel from

  • 0

As you can see in the following code, I am accessing a JLabel from the ActionListener anonymous inner class. This provides me with no errors, so how is this allowed but if the JLabel was INSIDE the method is is not allowed without the final modifier?

JLabel e = new JLabel("");
        public void myMethod() {

            JButton b = new JButton("ok");
            b.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    e.setSize(200,200);

                }

            });

        }
  • 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-06-03T20:13:06+00:00Added an answer on June 3, 2026 at 8:13 pm

    If the variable is inside the method def, then it’s a local variable — and you are instantiating an object that will exist after that method’s execution, and with it the lifetime of that local variable, has ended (it’s in the method’s stackframe that gets destroyed upon method return). For this to work at all some compiler magic is required, and this is usually called a closure, even if its current implementation in Java is so lame. The compiler will actually synthesize a class that implements ActionListener and has an instance variable to which the local variable’s value will be copied.

    It is a restriction specific to Java that you can only close over final vars due to thread-safety concerns. The story here goes that it is ingrained in the intuition of a Java developer that a local var is always thread-safe — it can’t change in the middle of method’s execution (with no change done explicitly by that method), there can be no dataraces on it, etc. This would be violated if you could close over non-final vars as that closure could be executed in parallel with the currently executing method and change that var. That would lead to some very counterintuitive behavior.

    There is, however, a (somewhat lame) workaround:

    public void myMethod() {
        final JLabel[] e = {new JLabel("")};
        JButton b = new JButton("ok");
        b.addActionListener(new ActionListener() {
           @Override public void actionPerformed(ActionEvent arg0) {
             e[0].setSize(200,200);
        }});
    }
    

    e is now a one-element array, which resides on the heap and doesn’t get destroyed with the method’s stackframe. You can also apply your intuition to see that now it’s definitely not thread-safe and that listener could easily change the value of that e[0] to something completely different, and the method shown here would observe that change with no explicit mutation code in it.

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

Sidebar

Related Questions

I have the following code from apache's svn. As you can see this is
i have written the following code: As you can see there is a for
I m having the following code, however, I can see that the radio button
How can I implement cross browser opacity gradient (not color gradient)? See following code:
I have the following problem. I am making this website you can see in
I have the following code at http://jsfiddle.net/qPtXL/ As you can see the problem is
Please see following code I want to know how can I de-alloc cell .
using the following code context.Response.StatusCode = 301; context.Response.Redirect(newUrl, true); context.Response.End(); I can see in
I have the following code. When I use breakpoints, I can see that [@myID].Value
In sqlite's source code, you can see following define: /* ** Add the ability

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.