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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:07:58+00:00 2026-05-14T19:07:58+00:00

Yes, the private member variable bar should be final right? But actually, in this

  • 0

Yes, the private member variable bar should be final right? But actually, in this instance, it is an atomic operation to simply read the value of an int. So is this technically thread safe?

class Foo {
    private int bar;
    public Foo(int bar) {
        this.bar = bar;
    }
    public int getBar() {
        return bar;
    }
}

// assume infinite number of threads repeatedly calling getBar on the same instance of Foo.

EDIT:

Assume that this is all of the code for the Foo class; any threads with a reference to a Foo instance will not be able to change the value of bar (without going to such lengths as using reflection etc.)

  • 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-14T19:07:58+00:00Added an answer on May 14, 2026 at 7:07 pm

    Final update: so my first conclusion happened to be right, just my reasoning was faulty 🙁 I re-edited my answer to make it somewhat coherent, not to hide the traces of my earlier blunder.

    Conclusion

    As @Wyzard pointed out, even though there is no way to change bar after construction, Foo is still not thread safe. The problem is not atomicity but visibility. If thread 1 is changing the value of bar in the constructor (from its default value of 0), there is no guarantee when other threads will get to see the new value (or whether they see it at all).

    So foo looks like an immutable object. Quoting from Java Concurrency in Practice, section 3.4:

    An object is immutable if:

    • Its state cannot be modified after construction;
    • All its fields are final; and
    • It is properly constructed (the this reference does not escape during construction).

    Foo looks OK on 1) and 3), but not 2). And that is a crucial point, due to the reasoning above. Declaring a variable final is one way of ensuring its visibility between different threads. The other means are declaring bar volatile, or synchronizing its access method(s). But of course, in case of an immutable object, neither of these would make much sense.

    Final Fields

    So why do finalfields guarantee visibility? Answer from Java Concurrency in Practice, section 3.5.2:

    Because immutable objects are so important, the JavaMemory Model offers a special guarantee of initialization safety for sharing immutable objects. As we’ve seen, that an object reference becomes visible to another thread does not necessarily mean that the state of that object is visible to the consuming thread. In order to guarantee a consistent view of the object’s state, synchronization is needed.

    Immutable objects, on the other hand, can be safely accessed even when synchronization is not used to publish the object reference. For this guarantee of initialization safety to hold, all of the requirements for immutability must be met: unmodi-fiable state, all fields are final, and proper construction. […]

    Immutable objects can be used safely by any thread without additional synchronization, even when synchronization is not used to publish them.

    This guarantee extends to the values of all final fields of properly constructed objects; final fields can be safely accessed without additional synchronization. However, if final fields refer to mutable objects, synchronization is still required to access the state of the objects they refer to.

    And what happens if the field is not final? Other threads may silently see a stale value of the field. There is no exception or any kind of warning – that is one reason why these kinds of bugs are so difficult to trace.

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

Sidebar

Related Questions

Yes, I know this question has been asked before, but I can't find an
Yes, I know this question has been asked a lot of times, but I
Yes, I have searched and tried many techniques, but nothing seems to work. Here
yes this is one of my homework projects - to implement a Circular Linked
private void frmSearch_Load(object sender, EventArgs e) { // TODO: This line of code loads
I noticed some people declare a private variable and then a public variable with
First of all, I'm sure there must be a simple solution to this but
I have following config in my POJO: @Field(index = Index.NO, store = Store.YES) private
There's a private method of the UIScrollView that should allow me to control the
My XML code is like this: <?xml version=1.0 encoding=utf-8?> <Tabel> <Member> <Naam>Cruciatum</Naam> <Kills>1000</Kills> <Deaths>10</Deaths>

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.