VOOZH about

URL: https://www.geeksforgeeks.org/java/final-arrays-in-java/

⇱ Final Arrays in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Final Arrays in Java

Last Updated : 23 Apr, 2026

In Java, the final keyword makes a variable's reference constant, not its contents. When an array is declared as final, you cannot reassign it to point to a new array. However, you can still modify the elements within the array.

  • A final array must be initialized once and cannot be reassigned later.
  • Helps maintain a fixed reference, improving code safety and predictability.

Final Array means

If an array is final, it means we can not make it point to a new array, but we can change the elements inside it.

Sample Example: final int[] arr = {10, 20, 30};

👁 Final-1
Array object in heap


Modifying elements inside the array is allowed

arr[2] = 99;

👁 Final-2
Modify array values

Reassigning the array reference is NOT allowed
arr = new int[]{600, 700, 800}; // Compilation error

Explanation: Here we have declared an array arr as final, which means we can not point arr to a different array, but we can change the value inside the array.

Examples of Final Arrays in Java

Let's now see some examples for better understanding.

Example 1: Modifying Elements of a Final Array


Output
10 20 99 

Explanation: The above example shows that we can change the values inside the final array but we can not replace the whole array.

Example 2: Modifying Object State Referenced by a Final Variable


Output
30

Explanation: The above example shows that we can change values inside a final object, but can not replace the object itself.

Example 3: Compilation Error When Reassigning Final Reference

Here is Java program to illustrate final arrays where compilation error is thrown

Output:

👁 Output1

Explanation: Above program compiles without any error and program 2 fails in compilation. Let us discuss why the error occurred. So a final array means that the array variable which is actually a reference to an object, cannot be changed to refer to anything else, but the members of the array can be modified. Let us propose an example below justifying the same.

Note: We cannot assign a value to final variable t1.

Example 4: Final Array Reference and Reassignment.

Here is Java Program to Illustrate Reassignment Error in Final Array

Output:

👁 Output2

Explanation: In the above example, we are trying to change a final array that why we are getting an error.

Example 5: Modifying Elements in a Final Array and Attempting Reassignment

Here is demonstrating how to change value inside a final array

Output:

👁 Output3
Output

Explanation: The above example shows that we can change the values inside a final array but we can not assign a new array to it.

What we can and cannot do with final arrays and objects in Java:

Operation

Allowed or Not

Explanation

Modify element in final array

Yes

Array contents are mutable.

Reassign final array to new array

No

The reference is final and it cannot point to a new object.

Modify object state in final variable

Yes

Object fields can be updated.

Reassign final object reference

No

Final variables cannot point to new instances.

Comment
Article Tags:
Article Tags: