0

I have a simple Java application that carries out all its logic in the main method of one class.

Currently I have just created methods in this class, declared them as Static and called them in the main.

Is this approach preferred over e.g. creating a "service" class then creating an instance of it to invoke my methods?

java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

1

If your methods are puerly functional (no side-effects), then it's ok to use static methods. If they have side-effects (such as changing static class members), this design is dangerous as other parts of the system may suddenly break.

A more general problem is that its hard to unit-test such static methods, as they cannot easily be mocked. I this case it is preferrable to use a stingleton implementing an interface. This singleton can then be mocked by a "dummy"-implementation

see also this question on SO: Why aren't static methods considered good OO practice?

Raphael Roth
  • 26,751
  • 15
  • 88
  • 145