I'm wondering if it is better to use a final static variable or method to provide a constant array in Java.
Say we have:
public class myColor {
public final static String[] COLOR_NAMES = new String[] {"red", "green"};
public final static String[] colorNames() {return new String[] {"red", "green"};}
}
To me, the second one has the disadvantage that each time it's called a new String array is created. But the first one has the disadvantage, that anyone could do COLOR_NAMES[0] = "blue".
To clarify: I specifically want to provide a list of color names for a subsequent match with regular expressions.
Is there any established way how this is typically solved?
Enum