• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to understand Static methods/objects

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic