How do you define a class of constants in Java?

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

    Use a final class. for simplicity you may then use a static import to reuse your values in another class

    public final class MyValues {
      public static final String VALUE1 = "foo";
      public static final String VALUE2 = "bar";
    }
    

    in another class :

    import static MyValues.*
    //...
    
    if(variable.equals(VALUE1)){
    //...
    }
    

提交回复
热议问题