aspose file tools
The moose likes Beginning Java and the fly likes Trying to understand Static methods/objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Trying to understand Static methods/objects" Watch "Trying to understand Static methods/objects" New topic
Author

Trying to understand Static methods/objects

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Hi all,

I am trying to learn a little bit about static methods and hope that someone here can help me understand it better. My understanding of static methods is that they exist only once (More like global methods). This tells me that usage of static methods should be used with care as they are likely to cause problems in cases where multiple users try to access a static object at the same time.

I am looking at a piece of code that had me thinking for a bit. I cant post the code itself but here is an example of how the code is structured

The first class declares a couple of non static arrays and makes them available via the getters and setters. It also has one method that calls a static method in another class.



The second class contains the static method that is called by the above class to join the two arrays. This class is used by many other classes



And here is my test class




The output to the above program is shown below



My question for the above is that i am wondering if the above is safe. Can you think of situations where the above is not recommended. What exactly would happen if ten instances of ClassA are executed at the same time? Woulnt the static method in ClassB corrupt the data?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24045
    
  13

Originally posted by O. Ziggy:
Hi all,

I am trying to learn a little bit about static methods and hope that someone here can help me understand it better. My understanding of static methods is that they exist only once (More like global methods). This tells me that usage of static methods should be used with care as they are likely to cause problems in cases where multiple users try to access a static object at the same time.


Be careful here -- you're inventing things. There's but a single copy of the code for all methods, static or not; and there's absolutely no problem in having any number of threads call a given method at once. The difference between a static and non-static method is that a non-static (or "instance") method has the "this" keyword available to refer to a "current object", while in a static method, there is no current object at all.

Where there can be problems is if multiple threads execute code that affects a single object -- or more properly, a single variable -- at the same time.


My question for the above is that i am wondering if the above is safe. Can you think of situations where the above is not recommended. What exactly would happen if ten instances of ClassA are executed at the same time? Woulnt the static method in ClassB corrupt the data?


The static method only operates on data that is passed in as arguments; there is no data that would be shared by all calls to this method. Therefore, it can be called by any number of threads at the same time, as long as each thread passes in its own data. Note that this really has nothing to do with the method being static or not.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Trying to understand Static methods/objects
 
Similar Threads
protected field
inheritance for Object class
Implementing Comparable Interface with TreeSets
ArrayList confusion
Linked List equals method