0

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?

4
  • 1
    This is example for using Enum Commented May 13, 2014 at 9:18
  • But the first one has the disadvantage, that anyone could do COLOR_NAMES[0] = "blue". Why is that a disadvantage? It's a final array isn't it? Commented May 13, 2014 at 9:19
  • Or use Enum... maybe ? Commented May 13, 2014 at 9:19
  • @cbach the array is final but not its content. Commented May 13, 2014 at 9:19

4 Answers 4

3

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;
  }
 }
Sign up to request clarification or add additional context in comments.

4 Comments

you can make color final and private.
How can I get a list / array of all the defined enum names?
Color.values() will return a Color[] of all the enums
Ok, but I would have to convert those to a String array --> not really better than using a method in the first place. Although I admit that enums are more elegant.
0

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(); }

2 Comments

An unmodifiable list is indeed maybe the best solution, thanks. Considering the second option, does it perform better, or is it a style thing? :-)
@Jay Style. It does exactly the same thing your code did, although if you ever had to add a color you'd only need to change one array instead of two.
0

This question is fall for using Enum. Personally I wont be using any method for that. But weather static array is good solution for that? It is better to solve it using java enums.

Comments

0

To initialise an array at construction time you can specify a list values in curly braces:

private static final String[] STRING_ARRAY = {"red", "green", "blue"};

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.

3 Comments

How does this answer the question? The OP shows that he already knows how to initialize an array at constant time, and he's interested providing a constant array.
Thanks for pointing out that new String[] is not necessary for static initialization. Didn't know that, but it does not answer my question.
@Jay I believe what Sanjay wrote is simply syntactic sugar; the new String[] is still there, but "hidden"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.