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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:10:39+00:00 2026-05-28T07:10:39+00:00

We all know you can’t do things like this: int a = 7; new

  • 0

We all know you can’t do things like this:

int a = 7;
new Runnable() {
     public void run() {
         System.out.println(a);
     }
}.run();
...

…without making a final. I get the technical reason why and it’s because local variables live on the stack and you can’t safely make a copy unless you know it won’t change.

What I struggle to see however is why the compiler doesn’t have an implementation hack so that it when it sees the above situation it compiles down to something like:

int[] a = {7};
new Runnable() {
    public void run() {
        System.out.println(a[0]);
    }
}.run();
...

Then we’re in the position where it’s safe to access a from an anonymous inner class and indeed change it if we wish. Of course, it might only do this hack when we actually change a. As far as I could see this would be a relatively simple thing to put in, would work for all types and would allow a to be changed from whatever context. Of course, the above proposal could be changed to use synthetic wrapper classes for multiple values or another approach that’s a bit more efficient, but the idea is the same. I guess there’s a small performance hit, but I doubt it’d be excessive especially with the potential for more optimisations under the hood. Aside from perhaps certain reflective calls that rely on synthetic fields being a certain way breaking, I can’t see many disadvantages, but I’ve never heard it seriously proposed! Is there a reason why?

  • 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-28T07:10:40+00:00Added an answer on May 28, 2026 at 7:10 am

    When the anonymous inner class is constructed, the values of all the variables used within it are copied. So if the inner class then tried to change the value of the variable, that wouldn’t be visible. For example, suppose this were valid:

    int a = 7;
    Runnable r = new Runnable() {
        public void run() {
            a = 5;
        }
    };
    r.run();
    System.out.println(a);
    

    You might expect it to print 5 (which indeed it would in C#). But because only a copy has been taken, it would actually print 7… if it were allowed, with no bigger changes.

    Of course, Java could have been changed to really capture the variable instead of its value (as C# was for anonymous functions). That requires automatically creating an extra class to store the “local” variables, and make both the method and the anonymous inner class share an instance of that extra class. That would have made anonymous inner classes more powerful, but arguably harder to understand. C# decided to go for the power-but-complexity route; Java went for the restrictive-but-simple approach.

    (Using an array instead of a custom class is valid for a single variable, but becomes more wasteful when there are multiple variables involved – you don’t really want to have to create a wrapper object for every variable if you can help it.)

    Note that there are significant complexities involved in the capture-the-variable approach, at least using the C# rules. For example:

    List<Runnable> runnables = new ArrayList<Runnable>();
    int outer = 0;
    for (int i = 0; i < 10; i++) {
        int inner = 0;
        runnables.add(new Runnable() {
            public void run() {
                outer++;
                inner++;
            }
        });
    }
    

    How many “inner” variables are created? One for each instance of the loop, or one overall? Basically, scopes make life tricky for this sort of thing. Feasible, but tricky.

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

Sidebar

Related Questions

We all know that you can overload a function according to the parameters: int
We all know the trouble overflows can cause, and this is why strn* exist
I know that you can run almost all Java in Dalvik's VM that you
We all know that we can declare multiple variables in a for loop like
What can you tell about modern data structures? We all know classic ones, like
We all know about semaphore and critical section problem. In pthreads, this can be
As we all know, in software dev, we can be asked very ambitious things
We all know calls like this: list($a, $b) = explode(':', 'A:B'); But how to
We all know that you can call $this->translate() within your zend view to translate
We all know you can't do the following because of ConcurrentModificationException : for (Object

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.