I have read the following discussions:
Should private helper methods be static if they can be static , and
Should all methods be static if their class has no member variables
It seems that people in general would accept static methods, but are a little bit skeptical about it, for the following 2 reasons:
- They are hard to test.
- They violate the OO principle. (They are functions, not methods, said a person.)
And the most acceptable static methods are private static ones. But then why do static methods exist at all, and in what situations they are the first priority to be adopted?
Think of this for a moment. In OO coding, every single function call actually looks like this:
method(object this, object arg1, object arg2)
where this is the object you are calling. All it really is is syntax sugar for this. Additionally it allows you to clearly define scope of variables because you have object variables etc.Static methods simply don't have a "this" parameter. Ie you pass variables in and possibly get a result back out. 1.) is the main reason people avoid them, you can't create an interface to a static method (yet) so you can't mock out the static method to test it.
Secondly OO are procedures functions etc. Static methods make a lot of sense in certain situations, but they can always be made into a method on an object.
Mind you, you couldn't remove this without a hack:
The code that starts your application MUST be callable WITHOUT a reference to an object. So they give you flexibility, whether you choose to use them in your scenario will be predicated by your requirements.
Another good scenario for static methods are implementations of the Factory pattern, where you are allowing for instances of a class to be constructed in a specific manner.
Consider the
Calendar
class, which has a set of staticgetInstance
methods, each returning instances primed with the desiredTimeZone
and/orLocale
or the default.I think a definite case for static methods is when you cannot make them dynamic, because you cannot modify the class.
This is typical for JDK objects, and also all objects coming from external libraries, and also primitive types.
It's much easier to refactor methods that are static. It discourages unnecessary references to field members that make the coupling tight. It's also easier to understand calling code because it explicitly passes any objects it interacts with.
First of all, you can't dismiss static-methods there is a reason people still use it.
some design patterns are based on static methods, singleton for example:
Config.GetInstance();
Helper functions, lets say you have a byte-stream and you want a function to convert it into a string of Hex numbers.
there are many uses for static-methods, saying that does not mean that some people abuse it too much(Code Review is best option when people abuse code).
Util classes can be OK to be static. As in someone elses example above, with escaping strings. The problem lies when those utils classes perform functions that we would want to mock. For example, the FileUtils class in apache commons - it is often the case that I want to mock file interactions without having to play with real files. If this was an instance class, it would be easy.