Is it bad practice for a class to have only static fields and methods?

后端 未结 14 701
予麋鹿
予麋鹿 2020-11-30 03:05

I have a class that consists only of static member variables and static methods. Essentially, it is serving as a general-purpose utility class.

Is i

14条回答
  •  自闭症患者
    2020-11-30 03:27

    Utility methods are often placed in classes with only static methods (like StringUtils.) Global constants are also placed in their own class so that they can be imported by the rest of the code (public final static attributes.)

    Both uses are quite common and have private default constructors to prevent them from being instantiated. Declaring the class final prevents the mistake of trying to override static methods.

    If by static member variables you did not mean global constants, you might want to place the methods accessing those variables in a class of their own. In that case, could you eleborate on what those variables do in your code?

提交回复
热议问题