wood burning stoves 2.0*
The moose likes Java in General and the fly likes Private static variables in a static class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Private static variables in a static class" Watch "Private static variables in a static class" New topic
Author

Private static variables in a static class

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
Are private static variables inside a static method thread safe?

I'm assuming that private static variables at the class level are not since they're properties of the class.

Look forward to any reply.

Thanks.

M
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi.

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.


Leandro Melo
SCJP 1.4, SCWCD 1.4
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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.

What do you think?

M
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18167
    
    8

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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
Thanks all for the great replies.

I'll move the class-level private static variables inside the static methods and call it day!!!

Thanks again.

M
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private static variables in a static class
 
Similar Threads
Help for the visibility of private access modifier
Private static variables in a static class
confusion in static
Encapsulation??
Why static variables and not allowed inside a method ?