To contradict other currently available answers: Static methods are bad!
They make a strong connection. Yes, there are times when this is acceptable. Yes, you can make a seam inside the static method using the strategy used inside the shift method. But, as a rule, static ones are still bad.
To answer the question of how to get rid of static methods. Simple: put them on the appropriate object. All statics are gone. Have we improved our code? not a lot yet. If we replace
callToStaticMethod()
with
new X().callToNoLongerStaticMethod()
we replaced the static call with a constructor call, which is essentially another static method. But now your X is just another dependency, so you can enter it:
class A{ private final X x; A(X aX){ x = aX; } }
Note: There is no need to use Spring or any other infrastructure for this. If it seems to you that it provides a constructor that uses the default implementation. If you are a purist, enter the interface for X.
Testing A , not relying on the implementation of X , becomes trivial and obvious. The same goes for replacing X any way.
source share