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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:32:06+00:00 2026-06-17T12:32:06+00:00

I got confused understanding the usage of the volatile keyword in Java. I have

  • 0

I got confused understanding the usage of the volatile keyword in Java. I have read many articles on the internet but am still getting nowhere. There are many questions in my head that I would like to ask:

First of all, Wikipedia and many blogs say that all volatile variables are stored in a Thread-Local memory over the main memory shared by all threads! I’m a little bit confused, are they meaning the stack memory? I know each thread has his own stack memory where it stores its own primitive literals & objects references.

If so, then what happen if the volatile variable is an object reference and not a primitive literal? I guess all the objects are stored in the heap space and not in the stack memory.

Second, could you please explain in details using an example how the volatile keyword works and when we are supposed to use it?

  • 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-17T12:32:07+00:00Added an answer on June 17, 2026 at 12:32 pm

    First of all, Wikipedia and many blogs say that all volatile variables are stored in a Thread-Local memory over the main memory shared by all threads.

    That is incorrect. Volatile fields are instance or class (static) variables and are stored in the heap.

    They might be referring to cache memory that is specific to a single processor / core … but that is a hardware-specific thing. But this is definitely not "thread-local". That term means something entirely different.

    If so, then what happen if the volatile variable is an object reference and not a primitive literal?

    Nothing special. Your assumptions are incorrect.

    Volatile variables are not stored on the stack. Indeed, if you attempt to use the volatile keyword for a local variable, you will get a compilation error. (It would make no sense. Variables on the stack are only visible to one thread. Volatile semantics are about variables that are shared by different threads.)

    I know each thread has his own stack memory where it stores its own primitive literals & objects references.

    What is stored on the thread’s stack is:

    • the method’s local variables,
    • the method’s parameters,
    • the method’s return address etc so that the CPU knows where to go when the call returns, and (possibly)
    • state of local objects that the JIT compiler has figured don’t need to be stored in the heap.

    A method’s primitive literals are typically embedded in the code itself. String literals are elsewhere too. (When those literals are assigned to local variables they will be held on the stack …)

    Second, could you please explain in details using an example how the volatile keyword works and when we are supposed to use it?

    Check the comments, and the related questions … or Google "java volatile example". An explanation would be redundant.

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

Sidebar

Related Questions

I have a very simple task but got confused. I need my action to
I've been following along with this excellent asio tutorial , but have got confused
I got confused with Git !! I have some files which have added and
In the book Programming Clojure(Stuart), when read how macros are expanded I got confused.
I got completely confused with gae. I have a script, that does a post
I have the following code and got myself confused: I have a query that
I have started programming recently and I got confused when I learnt about data
I was reading this Freuqent Java concurrency problems question and got confused by an
I have got confused due to what is true regarding the operator precedence in
i was dealing with the following code,& got confused,please have a look at it

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.