0

Usually we manage constants in interfaces. e.g.:

public interface CommonConstant {
    int A_TYPE1=0;
    int A_TYPE2=1;
}

But I'm thinking that if the constant belongs to a class, we may just put it into that class? e.g.:

public class A {
    public enum Type {
        TYPE_ONE(0),
        TYPE_TWO(1);

        public final int val;
        Type(int val) {
            this.val = val;
        }
    }

    private int type;
    ...
}

So which way should be better?

Zhi LI
  • 1
  • Better match : http://stackoverflow.com/questions/320588/interfaces-with-static-fields-in-java-for-sharing-constants?noredirect=1&lq=1 – NewUser Jan 20 '17 at 09:31
  • This is a very broad question, with possibilities from using `enum` to provide strong typing and additional functionality, the "standard" way of using a class or interface just for constants, using property files and additional mechanism to allow configuration on the fly etc. etc. – Kayaman Jan 20 '17 at 09:39
  • @Kayaman: Agreed! You have a gold badge, can you please close this question? – NewUser Jan 20 '17 at 09:40

0 Answers0