This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Marking a variable static final is thread safe Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Marking a variable static final is thread safe" Watch "Marking a variable static final is thread safe" New topic
Author

Marking a variable static final is thread safe

Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
Folks,

I have a bit confusion that if i mark a variable "static final ", is it threadsafe?

like: public satic final MyObject myObj;

_____________________________
Mintoo
SCJP 1.4
_____________________________
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
No, not generally.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I would say that the variable itself is threadsafe, but if it's a reference variable and the object it refers to is mutable, then that object may not be threadsafe. Unless it's designed to be threadsafe.

More generally, this is true for any final variable, not just static variables. If they're final and primitive, or final and referring to an immutable class, then they're thread-safe. Otherwise they're probably not thread-safe (unless you determine otherwise through more detailed analysis).


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Jim, good point.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Marking a variable static final is thread safe
 
Similar Threads
Using final as program-wide variables
Programming restrictictions
y this code dosnt throw exception?
switch
static member initialization