Is there anything wrong with a class with all static methods?

后端 未结 16 1045
余生分开走
余生分开走 2021-01-30 16:43

I\'m doing code review and came across a class that uses all static methods. The entrance method takes several arguments and then starts calling the other static methods passin

16条回答
  •  鱼传尺愫
    2021-01-30 17:07

    I'm not quite sure what you meant by entrance method but if you're talking about something like this:

     MyMethod myMethod = new MyMethod();
     myMethod.doSomething(1);
    
     public class MyMethod {
          public String doSomething(int a) {
              String p1 = MyMethod.functionA(a);
              String p2 = MyMethod.functionB(p1);
              return p1 + P2;
          }
          public static String functionA(...) {...}
          public static String functionB(...) {...}
     }
    

    That's not advisable.

    I think using all static methods/singletons a good way to code your business logic when you don't have to persist anything in the class. I tend to use it over singletons but that's simply a preference.

     MyClass.myStaticMethod(....);
    

    as opposed to:

     MyClass.getInstance().mySingletonMethod(...);
    

    All static methods/singletons tend to use less memory as well but depending on how many users you have you may not even notice it.

提交回复
热议问题