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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:43:12+00:00 2026-06-14T06:43:12+00:00

Assuming this class: public class AmIThreadSafe { private int a; private int b; AmIThreadSafe(int

  • 0

Assuming this class:

public class AmIThreadSafe {

    private int a;
    private int b;

    AmIThreadSafe(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

Assuming that instance’s reference to this class (declared as volatile) is accessible by some threads (leading to race condition) as soon as the this(reference) escapes:

volatile AmIThreadSafe instance = new AmIThreadSafe(1,2);

Here, I’m sure that the fact of assigning instance reference happens-before reading by threads.

But what about the AmIThreadSafe's fields?

Does the external volatile keyword also imply an happens-before relation concerning a and b fields?
Or is it rather possible to end up with any thread seeing stale values (default values 0 in this case since int) due to a potential statements reordering during constructor?

In other word, should I declare a and b final or volatile to prevent any surprises with the JMM or is just indicating volatile on the instance’s reference enough?

—————-UPDATED POST – A GOOD ANSWER:—————————-

The following article confirms by its sample that in my case, a and b are protected from JMM optimisations that prevent a permanent happens-before relation.

http://jeremymanson.blogspot.fr/2008/11/what-volatile-means-in-java.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-14T06:43:14+00:00Added an answer on June 14, 2026 at 6:43 am

    Declaring instance as volatile does not make its fields volatile, but if I understand your question correctly, then — yes, it’s enough in your case.

    Per §17.4.5 of the spec:

    • a volatile write in one thread happens-before any subsequent volatile read in another thread.
    • statements within the same thread have the happens-before relationship that you’d expect.
    • happens-before relationships are transitive.

    So, if a thread perceives instance as having been initialized, then the initialization of instance happened-before it, and the initialization of instance‘s fields happened-before that, so the thread will perceive instance‘s fields as having been initialized.

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

Sidebar

Related Questions

Assuming that I have objects similar to this: public class User { public int
Assuming I have a simple structure that looks like this: public class Range {
Assuming I have this class: class Shape { public: int value; Shape(int v) :
I am missing this DLL reference in c:\program files\SQL Server\90\Tools\Binn. I'm assuming that this
Like this: public class remoteStatusCounts : RemoteStatus { public int statusCount; public remoteStatusCounts(RemoteStatus r)
Assuming I have an Entity Framework 4.2 class like this: class Company { public
Assuming this: public class GenericMethods { public T Method<T>() where T : struct {
Assuming I have a class like this: class Database { public static $hostname =
Is this good OO Design assuming you want every inheriting class to be a
Assuming I have this HTML structure: <ul class='menu'> <li> <div></div> <div> <div></div> <div> <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.