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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:54:31+00:00 2026-06-18T06:54:31+00:00

So, here’s what I understand: Java doesn’t support closures, so it sort of copies

  • 0

So, here’s what I understand: Java doesn’t support closures, so it sort of copies the variables from the containing scope into the nested scope, so they are available later. Because this is a copy, there is no way to synchronize the original and the copy, and the variable is forced to be final so the developer cannot change it and expect it to be updated. This understanding is partly taken form these answers

And that brings us to this code working:

public class SimpleClosure {

    public static void main(String[] args) {
        new SimpleClosure().doStuff();
    }

    public void doStuff() {
        final int number = 3;

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Integer.toString(number));
            }
        }.start();
    }
}

So, great. Now, the thing is, a final modifier only prevents me from changing the object that the variable points to, but I can change the object without problems. If a “copy” was made, then changes to the object contents should not be reflected. The question, therefore is, why does the following code work?

import java.util.HashMap;
import java.util.Map;

public class StretchingClosure {

    public static void main(String[] args) {
        new StretchingClosure().doStuff();
    }

    public void doStuff() {
        final Map<String, String> map = new HashMap<String, String>();
        map.put("animal", "cat");

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(map.get("animal")); // Outputs dog See (1)
            }
        }.start();
        map.put("animal", "dog");
    }
}

Needless to say, I’m missing something, or I’m oversimplyfying the way the compiler handles these cases. Could anyone please enlighten me?

(1): as pointed out by @trashgod, the output is true most of the time on most of the platforms, but it’s not guaranteed due to the lack of synchronization. This is good enough for the example, but bad practice in general.

  • 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-18T06:54:33+00:00Added an answer on June 18, 2026 at 6:54 am

    Don’t confuse the variable with the object: the reference from the local variable is indeed copied, but it still refers to the same object, in your case the map. There is a widely known idiom to work around the final restriction, involving arrays:

    final int[] x = {1};
    ... use in an anonymous instance...
    System.out.println(x[0]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
Here is my query: select word_id, count(sentence_id) from sentence_word group by word_id having count(sentence_id)
Here's a sample of a SpinBox that writes its changes to underlying variables. The
Here is the code I have. This is my PrintToFile class import java.util.*; import
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here's the code I'm running. Basically I scrape data, and place them into simple
Here is the code from my project where I am using a datepicker. The
Here I am trying to retrieve the response from the server and display it,
Here's the scenario: I am using ctypes to load a C DLL into a

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.