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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static methods and thread safety" Watch "static methods and thread safety" New topic
Author

static methods and thread safety

Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 34
Hi all
Can you please tell me whether static mehods/variables are thread safe? if not why not?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

There's nothing about static methods or variables that makes them inherently thread-safe.

You can easily write some example code with two threads, in which the threads try to write to the same static variable at the same time, so that the end result is unpredictable - which demonstrates that it's not thread-safe.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: static methods and thread safety
 
Similar Threads
Static variable
Still Static Methods...
Static Variable in Servlet
Message doesnt display continuously
Mock Question about thread-safe