How to use enum to define a bunch of string consts
any example of using enum to define a bunch of string const开发者_如何转开发s? The string can contains special charaters like - / etc?
enum MyConstants {
STR1("some text"),
STR2("some other text");
private String value;
private MyConstants(String str) {
this.value = str;
}
public String getValue() {
return value;
}
}
then use it like this:
MyConstants.STR1.getValue();
String [] messages = {"maybe you", "better go with", "an array?"};
System.out.println (messages[1]);
Without further knowledge - why do you like to use enums at all?
I think this page will be helpful: http://javahowto.blogspot.com/2006/10/custom-string-values-for-enum.html
In short:
public enum MyType {
ONE {
public String toString() {
return "this is one";
}
},
TWO {
public String toString() {
return "this is two";
}
}
}
精彩评论