| 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.
|
 |
 |
|
|
subject: Marking a variable static final is thread safe
|
|
|