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?
We can have an array with strings as its elements. Thus, we can define a String Array as an array holding a fixed number of strings or string values. String array is one structure that is most commonly used in Java. If you remember, even the argument of the 'main' function in Java is a String Array.
To initialise an array at construction time you can specify a list values in curly braces: private static final String[] STRING_ARRAY = {"foo", "bar", "baz"}; In my example I have assumed that you won't want to change the instance of array and so have declared it final .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With