How do you define a class of constants in Java?

后端 未结 10 2046
终归单人心
终归单人心 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:20

    My preferred method is not to do that at all. The age of constants pretty much died when Java 5 introduced typesafe enums. And even before then Josh Bloch published a (slightly more wordy) version of that, which worked on Java 1.4 (and earlier).

    Unless you need interoperability with some legacy code there's really no reason to use named String/integer constants anymore.

提交回复
热议问题