Utility class in Spring application - should I use static methods or not?

前端 未结 3 1438
北海茫月
北海茫月 2021-01-30 12:23

Let\'s say I have a utility class DateUtil (see below). To use this method a caller method uses DateUtils.getDateAsString(aDate). Would it be better to remove the static modifie

3条回答
  •  独厮守ぢ
    2021-01-30 13:12

    I agree with Sean Patrick Floyd.

    This is my criterion: if the methods of the class do things only over the parameters they receive, with no external dependencies (database, file system, user config, other objects/beans, etc.), then I would do it with static methods, usually in a final class with a private constructor.

    Otherwise, I would implement it using a Spring bean.

    So, in the case that you raise, according to this criterion, I would write a class with static methods.

    Regards.

提交回复
热议问题