Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store constant array of string in Java

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?

like image 763
tsusanka Avatar asked Mar 19 '12 21:03

tsusanka


People also ask

Can you make an array of strings in Java?

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.

How do you declare a string array as static?

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 .


2 Answers

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.

like image 140
Jakub Zaverka Avatar answered Sep 19 '22 12:09

Jakub Zaverka


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.

like image 41
Jon Skeet Avatar answered Sep 20 '22 12:09

Jon Skeet