How to declare a static string array in Java? [closed]


I have a Java program that has two functions and a static string array. Can anybody tell me how to declare a static string array in Java?


Answers:


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 still would be able to update individual entries like so:

 array[0] = "1";

But you won't be able to replace the array with a different one completely. If the values are going to change a lot - especially if the number of values are going to change - then it may be worth considering using List instead.