The final array in Java

The following code in Java uses the final String array, and there is no doubt about it.

 final public class Main { public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"}; public static void main(String[] args) { for (int x = 0; x < CONSTANT_ARRAY.length; x++) { System.out.print(CONSTANT_ARRAY[x] + " "); } } } 

It displays the following output on the console.

 I can never change 



The following code also raises no questions.

 final public class Main { public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"}; public static void main(String[] args) { //CONSTANT_ARRAY={"I", "can", "never", "change"}; //Error - can not assign to final variable CONSTANT_ARRAY. for (int x = 0; x < CONSTANT_ARRAY.length; x++) { System.out.print(CONSTANT_ARRAY[x] + " "); } } } 

Obviously, a commented-out string causes the indicated error because we are trying to reassign the declared final array of type String .




How about the following code.

 final public class Main { public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"}; public static void main(String[] args) { CONSTANT_ARRAY[2] = "always"; //Compiles fine. for (int x = 0; x < CONSTANT_ARRAY.length; x++) { System.out.print(CONSTANT_ARRAY[x] + " "); } } } 

and displays I can always change means that we were able to change the value of the final array of type String . Can we ever change the entire array this way without breaking the final rule?

+49
java
Apr 26 '12 at 19:18
source share
8 answers

final in Java affects a variable; it has nothing to do with the object you assign to it.

 final String[] myArray = { "hi", "there" }; myArray = anotherArray; // Error, you can't do that. myArray is final myArray[0] = "over"; // perfectly fine, final has nothing to do with it 

Edit adding comments: Please note that I said the object that you assigned to it. In Java, an array is an object. The same applies to any other object:

 final List<String> myList = new ArrayList<String>(): myList = anotherList; // error, you can't do that myList.add("Hi there!"); // perfectly fine. 
+81
Apr 26 '12 at 19:23
source share

You misinterpret the final implementation. final is applied to the reference to the array object, which means that after it starts, the link can never change, but the array can be filled. “Without breaking the rules”, you specified only one rule regarding the reference change, which works accordingly. If you want the values ​​to never change either, you have to go to Immutable lists i.e.

 List<String> items = Collections.unmodifiableList(Arrays.asList("I", "can", "never", "change")); 
+15
Apr 26 '12 at 19:34
source share

You can make sure that the reference to the array cannot be changed. If you want the elements to not be modified, you need to use an unmodifiable collection.

+10
Apr 26 '12 at 19:22
source share

When you declare an array as final, you can change the elements in the array, however you cannot change the reference of this array.

+3
Apr 26 '12 at 19:24
source share

final guarantees the immutability of primitives. It also ensures that a variable is assigned only once. If an object is modified, you can change the contents of its event, which it defined as final. You can check immutable collections for your needs. For example, Collections.unmodifiableList () http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#unmodifiableList(java.util.List )

+2
Apr 26 '12 at 19:26
source share

The reference to the array object is final (cannot be changed, for example, if you try to associate another Java array object (an instance of String []) with the same final variable ... you will get a compilation time error).

BUT the fields of the final array object in your example are not final, so you can change their value .... while the created Java object, CONSTANT_ARRAY, after receiving the initial value, will have this value "forever" == until until the JVM stops. :) It will be the same String Array instance forever.

Final variables in Java is not a big problem, just take some time to carefully revise the topic / idea. :-)
I suggest to all those who are not sure about the meditation on this page, for example: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4

Let me give you the relevant part:

When the final variable has been assigned, it always contains the same value. If the final variable contains a reference to the object, then the state of the object can be changed using operations on the object, but the variable will always refer to the same object.

This also applies to arrays, since arrays are objects; if the final variable contains a reference to the array, then the components of the array can be changed using operations on the array, but the variable will always refer to the same array. "

+2
Sep 13 '15 at 22:59
source share

The value of the CONSTANT_ARRAY variable cannot change. This variable contains an array (reference to). However, the contents of the array may change. The same thing happens when you declare some final variable that is not a simple scalar type (for example, an object).

Be careful what you name the variables. :-) Calling CONSTANT_ARRAY does not make the contents of the array unchanged.

Here's a good link: Last word in the finale

+1
Apr 26 '12 at 19:25
source share
  final int[] res; int[] res1; int[] res2 = new int[1]; res2[0]=20; res1=res2; res1=res2;//no error System.out.println("res1:"+res1[0]); res = res2;//only once //res = res2;//error already initialised res2[0]=30; System.out.println("res:"+res[0]); 

Output :: res1: 20 resolution: 30

-one
Sep 18 '17 at 4:44 on
source share



All Articles