Java array of constants

51,113

Solution 1

If you want to create an immutable array, no, you cannot. All arrays in Java are mutable.

If you just want to predefine the array in your class, you can do it:

private static final int[] MY_ARRAY = {10, 20, 30, 40, 50};

Here we created a predefined array MY_ARRAY of length 5, so MY_ARRAY[0] is 10 and so on. Be careful though as even the MY_ARRAY field is declared final, this does not mean that array elements could not be changed. Thus it's better not to expose such array to public via public or protected modifier.

Solution 2

If you don't want to modify the values, and you also just want to access the members of the collection without wanting random access, a very simple solution instead of having a constant array, have a final immutable list:

static final ImmutableList<Type> arrayOfConstants = ImmutableList.of(t1, t2, t3);

Solution 3

I mean the array components be constants i.e. a[0] be a constant variable like this public static final int SIZE = 10;

You cannot give array indexes names.

You could initialize an array using the values of pre-existing constants:

public static final int SIZE = 10;

public static final int[] CONSTANTS = { SIZE };

Keep in mind that although an array is declared final, it's values may still be changed. final only ensures you cannot re-assign the array variable, so you will want to encapsulate the array to prevent mutation:

final class Constants {
    public static final int SIZE = 10;

    private static final int[] CONSTANTS = { SIZE };

    public static int getConstant(int index) {
       return CONSTANTS[index];
    }
}

If you would like to loop, I suggest returning a deep-copy of the array.

Solution 4

if final is used with objects you cannot change the reference of that object but changing the value is perfectly fine.Array is an object in java and if you want object value should not be changed, once created, then you will have to make object immutable and primitive array cannot be made immutable in java.

    final int [] finalArr={5,6,8};

     System.out.println("Value at index 0 is "+finalArr[0]);    
      //output : Value at index 0 is 5

      //perfectly fine
      finalArr[0]=41;

     System.out.println("Changed value at index 0 is "+finalArr[0]);
    //Changed value at index 0 is 41

     int[] anotherArr={7,9,6};
     // finalArr=anotherArr;
     //error : cannot assign a value to final variable finalArr

For more on immutable array you can refer to these links:

Immutable array in Java

Is there any way to make an ordinary array immutable in Java?

Share:
51,113
Athanasios V.
Author by

Athanasios V.

Updated on July 09, 2022

Comments

  • Athanasios V.
    Athanasios V. almost 2 years

    How is it possible to declare and initialize an array of constants in Java, without using enums ?

    static final Type[] arrayOfConstants = new Type[10]; // not an array of constants
    
    • Dioxin
      Dioxin over 8 years
      You mean ensure the values of the array cannot be changed? You will need to encapsulate the array in an object that prevents mutation but allows access by index via getter method
    • Athanasios V.
      Athanasios V. over 8 years
      I mean the array components be constants i.e. a[0] be a constant variable like this public static final int SIZE = 10;
    • Dioxin
      Dioxin over 8 years
      I'm not sure what you mean. You want to give the array indexes names?
    • Athanasios V.
      Athanasios V. over 8 years
      @Vince Emigh Your answer before makes sense to me.
    • Dioxin
      Dioxin over 8 years
      What answer? The only answer I have is down below, which you can kindly choose as accepted answer if it helped with your issue :) Unless you're referring to my comment, which in that case my answer includes that info (with examples), and you should check it out
    • niceman
      niceman almost 8 years
      I think there are Immutable Collections in java, but not arrays unfortunately
  • Athanasios V.
    Athanasios V. over 8 years
    No this is not an array of constants, this is a constant array. i mean the components of the array be constants not the array itself
  • Tagir Valeev
    Tagir Valeev over 8 years
    @AthanasiosV., please reread the very first sentence of my answer.
  • Athanasios V.
    Athanasios V. over 8 years
    Yes your statement is absolutely right sorry for me quick response
  • Dave L.
    Dave L. about 7 years
    ...except for zero-length arrays, which are safely immutable.
  • Nic
    Nic over 5 years
    The question wasn't "how do I give array indices names", it was "how do I make an array of constants, this thing shows what I mean by constant". Your answer says "you can't do this unrelated thing, oh and by the way you should keep in mind this caveat which is literally the entire center of your question".