Making all methods static

后端 未结 7 2086
-上瘾入骨i
-上瘾入骨i 2020-12-25 08:20

In my application there are many classes which contain methods for processing data which can be computation and data enrichment.

My question is - if the class doesn\

7条回答
  •  [愿得一人]
    2020-12-25 08:44

    You can definitely make all the methods static. Why not? If the language allows it, and it makes sense to you, do it. I've rarely benefited from doing something that didn't make sense to me just because I'd not seen that done before or because it didn't follow some design pattern (like "OO"—that means a lot of things to many people, and Java—especially Java 8—actually supports many paradigms in addition to OO). I definitely wouldn't add lines of code that aren't necessary (Obj a = new Obj(); a.do() vs Obj.do()).

    In terms of prior art, such classes are often called "utility classes". But there's nothing that has to be utility about such a class. Personally, I use static methods when I see no need to capture values or references within a class instance. There are many times this arises for me, and I've seen this all over in other people's programming as well.

提交回复
热议问题