squid:S2386 - array of strings

public static final String[] ARRAY_CONSTANT = new String[] { "this", "will", "never", "change" };

raises squid:S2386, even that it really cannot change.
I think there should be exception for immutable objects (like String here) - it doesn’t make sense to provide a getter for such a constant.

I would understand it if it’s a collection, not base array, or if it’s an array of mutable objects.

SonarLint 4.1 for IntelliJ.

Can you try an immutable list? https://stackoverflow.com/questions/3700971/immutable-array-in-java

hello @noelo_cohelo,

I think the rule still has merit, try to execute following snippet

import java.util.Arrays;

class Scratch {

  public static final String[] ARRAY_CONSTANT = new String[] { "this", "will", "never", "change" };

  public static void main(String[] args) {
    System.out.println(Arrays.toString(ARRAY_CONSTANT));
    ARRAY_CONSTANT[2] = "maybe";
    System.out.println(Arrays.toString(ARRAY_CONSTANT));
  }
}
1 Like