Remember that the difference between a static and non-static methods is whether there is an instance (object) of that class as an implicit parameter to the method. Static methods are often used to produce instances, or for algorithms that don't depend on having an object. (Math comes to mind).
hi Namaste Sathi what is the typical situation where static method in a class is required? The static method can be used to return the static variable.
It can also be used to share the process of operation to other objects(remember that it is not the individual instance method) I think that use the class method can reduce more memory space than using instance method if many object created.(It is related to performance) My knowledge is limit in the performance,but Jim knows more about the knowledge of performance
Static methods get into some controversial areas in OO discussions. One thing that distinguishes OO from procedural code is that an object encapsulates behavior plus state. A lot of static methods have no state. Integer.parseInt has an input parameter and output return value, but does not deal with any object state. It can be argued (I'm not takin sides here!) that when you have no state, you have a poor object. Some OO theorists are happy to catalog these as "function" objects or some such thing. Some static methods do have static state. Caches are a good example - you only want one copy of the cache so static works. Others could go either way, but using static methods saves the client the effort of creating an instance. Frinstance I made a logger with static methods so you could do:
And some could argue that I was just being lazy. And this prevents me from having two loggers with different configurations at the same time. One can also make a strong argument that static data is just poorly disguised global data, with all the problems that come with globals. Hope that helped.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi