This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
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 Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static methods and thread safety" Watch "static methods and thread safety" New topic

static methods and thread safety

Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 55

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: 15047

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 8 API documentation
I agree. Here's the link:
subject: static methods and thread safety
It's not a secret anymore!