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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:46:42+00:00 2026-06-14T16:46:42+00:00

I am trying to understand exactly how element visibility works on arrays in java.

  • 0

I am trying to understand exactly how element visibility works on arrays in java.

Given the class:

class IntList {

    private final int[] array;

    public IntList(int[] array) {
        this.array = array;
    }

    public int[] readElements() {
        return Arrays.copyof(this.array, this.array.length);
    }

}    

and the following method body for creating an instance:

int[] array = new int[length];
fillArrayWithRandomData(array); // puts data into the array from arbitrary source
return new IntList(array);

I am wondering if the elements in the IntList are guaranteed to be visible by other threads that obtain a reference to the returned IntList?

I am sure that the REFERENCE to the array will be visible because it is final but I cannot seem to find a guarantee that the elements in the array will be visible as well.

Note: The IntList class has no methods that allow the modification of the array and the array reference is not published to any other object, I am only wondering about visibility after construction.

Edit: Sorry, my class is not called String in my actual implementation. I changed the class name to IntList because there seems to be too much confusion.

Edit:
The final answer I’ll put here is Yes, the elements are visible.
@MikeClark found the JLS answer:
JLS § 17.5 “The usage model for final fields is a simple one: Set the final fields for an object in that object’s constructor; and do not write a reference to the object being constructed in a place where another thread can see it before the object’s constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object’s final fields. It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are.”

Thanks again!

  • 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-14T16:46:43+00:00Added an answer on June 14, 2026 at 4:46 pm

    Since you are populating the array in the constructor then yes, any call to new String(int[] array) will have the array initialized by the time it returns. The final keyword will also guarantee that the most recent changes to the array parameter at the time of assignment will be visible.

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

Sidebar

Related Questions

I'm trying to understand how this exactly works (I know what it does, I
I program in Java and have been trying to understand exactly what operator overloading
I'm trying to understand exactly how the following code snippet works, in particular line
I'm trying to understand what exactly the above (in my question's Title) means? This
I am learning socket programming in c and trying to understand what exactly is
I am trying to understand a code, what exactly &(ipA[iLower + 1] stands for
I'm trying to understand some MATLAB source codes. I don't know exactly which one
Can someone explain this to me? I'm trying to understand exactly why drop down
I'm trying to understand how exactly a cache miss occurs. So far this is
I'm trying to better understand what exactly this code is doing. It's written in

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.