Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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?

like image 238
user1407310 Avatar asked Oct 23 '25 01:10

user1407310


2 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.

like image 114
Dave Webb Avatar answered Oct 24 '25 14:10

Dave Webb


public static String[] stringArray = new String[size]; // give some "size"

OR

public static String[] stringArray = {"String1","String2","String3"}; 
like image 45
AllTooSir Avatar answered Oct 24 '25 14:10

AllTooSir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!