This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static method" Watch "Static method" New topic
Author

Static method

Paul Medford
Ranch Hand

Joined: Aug 28, 2001
Posts: 33
I have a doubt now...
Say you have a util abstract class with static methods. Whenever a method is called, is it thread safe? or is there a chance that the same method be called more than once at the same time and corrupting the data?
Simple question, but I'm kind of puzzled...
Thanks,
Francois
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Francois,
The only way to make a method thread safe is to make the methods synchronized.
Example (thread safe ):
java.util.Vector
Example (not thread safe):
ArrayList
Regards,
Manfred.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Or make sure that you only access local variables in it and not access any instance or class variables.
Junilu
Paul Medford
Ranch Hand

Joined: Aug 28, 2001
Posts: 33
Thanks, it's what I thought. I just had a doubt, somehow...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static method
 
Similar Threads
Static Methods
Difference between Hidden and overriden methods.
Using static methods
Static Methods in Java
JavaCaps mock 1 Q:39