File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 15081

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!