If you are creating an array then there is no difference, however, the following is neater:
String[] suit = {
"spades",
"hearts",
"diamonds",
"clubs"
};
But, if you want to pass an array into a method you have to call it like this:
myMethod(new String[] {"spades", "hearts"});
myMethod({"spades", "hearts"}); //won't compile!
Answer from dogbane on Stack OverflowIf you are creating an array then there is no difference, however, the following is neater:
String[] suit = {
"spades",
"hearts",
"diamonds",
"clubs"
};
But, if you want to pass an array into a method you have to call it like this:
myMethod(new String[] {"spades", "hearts"});
myMethod({"spades", "hearts"}); //won't compile!
Nope, no difference. It's just syntactic sugar. Arrays.asList(..) creates an additional list.
how to initialize a static array of objects in java - Stack Overflow
How to declare a static string array in Java? - Stack Overflow
Use a static variable or method for a constant array in Java - Stack Overflow
In Java, how do static arrays of Strings work? Strings are arrays of characters, so when the array of Strings is initialized with a given size, how does Java know how much memory to allocate?
Videos
You have to make LineType a static class:
public class Outer {
public static class LineType {
int id;
String descr;
private LineType( int a, String b) {
this.id = a;
this.descr = b;
}
}
static LineType[] myList = {
new LineType( 1, "first" ),
new LineType( 2, "second" ),
};
}
Unless there is something I'm not getting, this should be as simple as:
Object[][] mylist = {{1, "first"}, {2, "second"}};
update 2024
Somebody upvoted this very old answer. Java has advanced since 2012 and the answer now would look more like:
record Linetype(int id, String descr){};
static List<Linetype> mylist = List.of(
new Linetype(1, "first"),
new Linetype(2, "second)
);
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"};
You can use enum
public enum Color{
RED("red"), GREEN("green");
final String color;
Color(String color) {
this.color=color;
}
public String getColor() {
return color;
}
}
If you can go with a list, one option would be:
public final static List<String> COLOR_NAMES = Collections.unmodifiableList(
Arrays.asList("red", "green"));
You can always get an array if needed:
String[] array = COLOR_NAMES.toArray(new String[0]);
Otherwise your second option is fine although I would write it:
private final String[] COLOR_NAMES = {"red", "green"};
public static String[] getColorNames() { return COLOR_NAMES.clone(); }
So essentially it is a 2D array, in which one dimension is fixed (the size of the array of strings) and one is variable (the size of the character arrays), correct?
String[] myStrings = new String[10]; // how much memory is allocated for myStrings?