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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:10:11+00:00 2026-05-28T04:10:11+00:00

Does making a class field volatile prevent all memory visibility issues with it in

  • 0

Does making a class field volatile prevent all memory visibility issues with it in a concurrent situation ? Is it possible that for below class , a thread that gets a reference of a Test object sees x as 0 first ( the default value of int ) and then 10 ? I am thinking this to be possible if and only if the Constructor of Test gives away this reference without completing ( improper publishing ) . Can someone validate/correct me ?

class Test {
    volatile int x = 10;            
}

Second question: what if it was final int x=10; ?

  • 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-28T04:10:11+00:00Added an answer on May 28, 2026 at 4:10 am

    You are actually not guaranteed to see x = 10 according to the JMM.

    For example if you have

    Test test =  null;
    
    Thread 1 -> test = new Test();
    Thread 2 -> test.x  == // even though test != null, x can be seen as 0 if the
                           // write of x hasn't yet occur
    

    Now if you had

    class Test{
      int y = 3;
      volatile x = 10;
    }
    

    If thread-2 reads x == 10 thread-2 is guaranteed to read y == 3

    To answer your second question.

    Having a final field will issue a storestore after the constructor and before publishing so having the field final will actually ensure you see x = 10.

    Edit: As yshavit noted. You lose the happens-before relationship I mention in my first example with final fields, that is as yshavit put it if thread-2 reads x == 10 it may not read y == 3 where x is a final field.

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

Sidebar

Related Questions

I am making an application that does some custom image processing. The program will
I'm making a simple scheduler with C# in .Net. All it does is execute
I have a ModelForm field that is based on the following Model: class Phrase(models.Model):
Does anybody know the logic behind making DataSourceSelectArguments sealed? I've implemented a custom DataSource
Does anyone have any tips regarding making animated GIFs in R? I'm trying to
Does anyone have links to documentation or guides on making the decision between REST
Does anyone have any good starting points for me when looking at making web
Does anyone know how to create your own JPA provider? I was considering making
a)In this case does the random number generator uses the system's clock (making the
Hibernate does not allow me to persist an object that contains an null embedded

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.