• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Private static variables in a static class

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes... sorry for that inaccurate answer Mike. Paul and Jim are correct (I imagined a restricted scenario when replying to your question).
reply
    Bookmark Topic Watch Topic
  • New Topic