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?
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.
public static String[] stringArray = new String[size]; // give some "size"
OR
public static String[] stringArray = {"String1","String2","String3"};
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