This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Since the variable is private, only its class is allowed to modify it. If you have only one static method that manipulates this variable I'd say yes, it's thread-safe, because this variable is not accessible through other means. However, if you have other static methods manipulating this same variable, things change and you might need to handle multi-threading issues.
SCJP 1.4, SCWCD 1.4
Joined: Jul 12, 2002
Thanks...wouldn't the best solution be to just include the class' static private static variables inside the static method that uses them?
That way, if you needed a similar variable, there wouldn't be a temptation to reuse it from another static method in the same class.
Sorry, but that is not correct. Suppose that private static variable is accessed by a public static method. Then it is possible for two threads to call that method at the same time. That situation is not thread-safe unless the method is synchronized.
It makes no difference whether the method or the variable are static or not.
Adding a little more... it also makes no difference whether there's one method, or two. What's important is whether or not more than one thread can access the method or methods. If so, then the method(s) must be written in a thread-safe manner, using synchronization or other techniques.
[Mike]: Thanks...wouldn't the best solution be to just include the class' static private static variables inside the static method that uses them?
It depends on what you mean by "include". If you make the variable a local variable inside the method, instead of a class or instance variable, then yes, that will work great. But if you use any non-final instance or class variable, at all, then you need to take additional steps (usually, synchronization) to make it thread-safe.
The other techniques are more complicated to figure out how to use safely. They include using volatile or atomic data types, or possibly ThreadLocal or other ways to restrict thread access to mutable data. The details are best discussed by a book such as Java Concurrency in Practice or Java Threads. [ June 01, 2007: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Joined: Jul 12, 2002
Thanks all for the great replies.
I'll move the class-level private static variables inside the static methods and call it day!!!