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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:45:04+00:00 2026-06-13T14:45:04+00:00

In Java Concurrency In Practice , the following example is given to illustrate how

  • 0

In Java Concurrency In Practice, the following example is given to illustrate how to create an immutable class:

http://www.javaconcurrencyinpractice.com/listings/ThreeStooges.java

This class has: private final Set<String> stooges = new HashSet<String>(); which is initialized in its Constructor:

public ThreeStooges() {
    stooges.add("Moe");
    stooges.add("Larry");
    stooges.add("Curly");
}

and has a method

public boolean isStooge(String name) {
    return stooges.contains(name);
}

to see if a name is one of the three stooges.

But when I do this: ThreeStooges ts = new ThreeStooges(), is it guaranteed that the object will be properly constructed (i.e. the state of stooges correctly initialized) before its reference is set into ts?

In other words, if I publish this object, is it possible that some thread will see it as incorrectly initialized (i.e. it will see stooges as empty when accessed through isStooge())?

My understanding is that an immutable object will be properly constructed and correctly visible when it’s published – (because it uses final instance variables). Is my understanding correct? If yes, is this class still immutable?

EDIT: it seems from the comments I saw that it’s difficult to believe an object can be seen by other threads before its Constructor completes. Here’s a link on that: http://jeremymanson.blogspot.in/2008/05/double-checked-locking.html

  • 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-13T14:45:05+00:00Added an answer on June 13, 2026 at 2:45 pm

    A whole heap of wrong answers here 🙁

    The initialization safety guarantees for final fields in the Java Memory Model are surprisingly strong. Not only do they guarantee that writes to final fields in the constructor are visible to any thread which obtains a shared reference to the object (even if that reference is obtained via a data race), but they guarantee that any writes in the constructor through that reference are visible to reads through that reference. The only caveat is that the reference to the object under construction not escape during construction. Of course, if the class were to mutate the object after construction, or offer a way for clients to get at the object reference to the HashSet, all bets are off.

    The purpose of this guarantee is to prevent the need for tricky reasoning about the state of immutable (in this case, effectively immutable) objects. IF the field is final AND there are no writes to the referred-to- object’s state other than those in the constructor, you’re done.

    If this makes your head hurt, don’t worry. If you (a) make the reference field private and final and (b) do not modify the state of the referred-to object outside the constructor and (c) do not provide any access that would let a client do the same (e.g., no mutative methods, no getter that exposes the field, etc), you’re done.

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

Sidebar

Related Questions

Java Concurrency in Practice gives the following example of an unsafe class which due
I am reading Java Concurrency in practice and looking at the example code on
I am reading Java Concurrency in practice and looking at the example code on
I'm studying Java Concurrency in Practice and there it is explained why the following
This is the implementation of the BoundedExecutor class in the Java Concurrency in Practice
I am reading the book Java Concurrency in Practice session 4.3.5 @ThreadSafe public class
In Brian Goetz's book, Java Concurrency in Practice, his example of a Reentrant lock
In the book Java Concurrency In Practice it explains the advantages of effectively immutable
Possible Duplicate: Thread safety in Java class I'm reading Java concurrency in Practice ,
In the book Java concurrency in practice , there is an example of customized

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.