Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the use of having a synchronized static method?  RSS feed

 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I undesrtand that the use of having a synchronized method is, no 2 threads will try to execute one method at a time and corrupt the state of an object. So synchronized is useful when it come to instance methods. But what's the use of having a synchronized static method?
Thanks
JEAN.
 
Paul Clapham
Sheriff
Posts: 22195
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the same reason. Why do you think there is a difference? Threads can execute static methods just as easily as instance methods.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronizing static methods is useful when the method accesses a shared "static" resource, such as a static field, an external resource etc.

A static method synchronizes on the Class object of the class it is declared on (instead of "this").
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!