How do you define a class of constants in Java?

后端 未结 10 2052
终归单人心
终归单人心 2020-12-07 12:44

Suppose you need to define a class which all it does is hold constants.

public static final String SOME_CONST = \"SOME_VALUE\";

What is the

10条回答
  •  盖世英雄少女心
    2020-12-07 13:28

    1. One of the disadvantage of private constructor is the exists of method could never be tested.

    2. Enum by the nature concept good to apply in specific domain type, apply it to decentralized constants looks not good enough

    The concept of Enum is "Enumerations are sets of closely related items".

    1. Extend/implement a constant interface is a bad practice, it is hard to think about requirement to extend a immutable constant instead of referring to it directly.

    2. If apply quality tool like SonarSource, there are rules force developer to drop constant interface, this is a awkward thing as a lot of projects enjoy the constant interface and rarely to see "extend" things happen on constant interfaces

提交回复
热议问题