Reputation: 4841
I am choosing randomly from set of particular strings in my application. I store those data in the code directly. As far as I know, you can't declare public static final String[] = {"aa", "bb"}
. So I though the enum would be useful, which works fine with one-word names:
public enum NAMES {
Mike, Peter, Tom, Andy
}
But how do I store sentences like this? Here the enum fails:
public enum SECRETS {
"George Tupou V, the King of Tonga, dies in Hong Kong at the age of 63.",
"Joachim Gauck is elected President of Germany.",
"Lindsey Vonn and Marcel Hirscher win the Alpine Skiing World Cup.";
}
What else should I use? Or am I using the enum incorrectly?
Upvotes: 14
Views: 58438
Reputation: 8874
You can do
public static final String[] = {"aa", "bb"};
you just need to specify the name of the field:
public static final String[] STRINGS = {"aa", "bb"};
EDIT: I second Jon Skeet's answer that this is bad code practice. Anybody can then modify the contents of your array. What you can do is declare it private and specify a getter for the array. You will preserve the index-access and prevent accidental writes:
private static final String[] STRINGS = {"aa", "bb"};
public static String getString(int index){
return STRINGS[index];
}
I guess you would need a method to get the length of the array as well:
public static int stringCount(){
return STRINGS.length;
}
But as long as your project is small and you know what you are doing, you will be perfectly fine just leaving it public.
Upvotes: 25
Reputation: 1500475
You can't create an immutable array, basically. The closest you can come is to create an immutable collection, e.g. with Guava.
public static final ImmutableList<String> SECRETS = ImmutableList.of(
"George Tupou V, the King of Tonga, dies in Hong Kong at the age of 63.",
"Joachim Gauck is elected President of Germany.",
"Lindsey Vonn and Marcel Hirscher win the Alpine Skiing World Cup.");
You could use an enum
by giving each enum value an associated string, like this:
public enum Secret {
SECRET_0("George..."),
SECRET_1("Joachim..."),
SECRET_2("Lindsey...");
private final String text;
private Secret(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
... but if you just want the strings as a collection, I'd use the immutable list. Enums are great when they're appropriate, but there's no indication that they're really appropriate in this case.
EDIT: As noted in another answer, this is perfectly valid:
public static final String[] FOO = {"aa", "bb"};
... assuming it's not in an inner class (which you didn't mention anywhere in your question). However, it's a very bad idea as arrays are always mutable. It's not a "constant" array; the reference can't be changed, but other code could write:
WhateverYourTypeIs.FOO[0] = "some other value";
... which I suspect you don't want.
Upvotes: 7
Reputation: 54
Insert the selected sentences in a Set<String>
and then use the return value of Collections.unmodifiableSet()
. Example:
final Set<String> mutableSentences = new HashSet<>();
/* ... */
final Set<String> sentences = Collections.unmodifiableSet(mutableSentences);
Upvotes: 0
Reputation: 5094
public enum NAMES {
Mike("Mike Smith"),
Peter("Peter Jones"),
Tom("Thomas White"),
Andy("Andrew Chu");
private final String fullname;
private NAMES(String value)
{
fullname = value;
}
};
Upvotes: 0
Reputation: 359816
public enum Secret
{
TONGA("George Tupou V, the King of Tonga, dies in Hong Kong at the age of 63."),
GERMANY("Joachim Gauck is elected President of Germany."),
SKIING("Lindsey Vonn and Marcel Hirscher win the Alpine Skiing World Cup.");
private final String message;
private Secret(String message)
{
this.message = message;
}
public String getMessage()
{
return this.message;
}
}
Upvotes: 6