Can we assign values to final arrays in Java?

Java 8Object Oriented ProgrammingProgramming

The array is a container that can hold a fixed number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. The following are the important terms to understand the concept of Array.

  • Element − Each item stored in an array is called an element.

  • Index − Each location of an element in an array has a numerical index, which is used to identify the element.

The size of the array will be determined at the time of creation.

Example

 Live Demo

public class ArrayExample {
   public static void main(String args[]){
      //Declaring an array
      int[] myArray = {233, 783, 453};
      //Printing the array
      for(int i=0; i<myArray.length; i++){
         System.out.println(myArray[i]);
      }
   }
}

Output

233
783
453

Final variables

If you declare a final variable later on you cannot modify or, assign values to it. Moreover, like instance variables, final variables will not be initialized with default values.

Therefore, it is mandatory to initialize final variables once you declare them.

Still, if you try to declare final variables without initialization that will generate a compilation error saying “variable variable_name not initialized in the default constructor”.

Example

public class FinalVariables {
   public static void main(String args[]){
      final int j = 10;
      j = 220;
   }
}

Final array

But in case of arrays you can declare arrays final and still can reassign values to it without any compile-time errors.

Example

 Live Demo

public class FinalArrays {
   public static void main(String args[]){
      //Declaring an array
      final int[] myArray = {233, 783, 453};
      //Printing the array
      for(int i=0; i<myArray.length; i++){
         System.out.println(myArray[i]);
      }
      //Changing the values of the array
      myArray[0] = 2020;
      myArray[1] = 3040;
      myArray[2] = 4060;
      for(int i=0; i<myArray.length; i++){
         System.out.println(myArray[i]);
      }
   }
}

Output

233
783
453
2020
3040
4060

The reason for this is unlike variables, arrays are objects and they do not hold values instead, they point to the address of the locations that hold values.

In the case of objects and arrays if a reference variable is final it cannot point to another object/array. If you try to do so a compile-time error is generated.

Example

 Live Demo

public class FinalArrays {
   public static void main(String args[]){
      //Declaring an array
      final int[] myArray1 = {233, 783, 453};
      //Printing the array
      System.out.println(myArray1);
      int[] myArray2 = {233, 783, 453};
      myArray2[0] = 2020;
      myArray2[1] = 3040;
      myArray2[2] = 4060;
      myArray1 = myArray2;
   }
}

Output

Compile time error

inalArrays.java:14: error: cannot assign a value to final variable myArray1
   myArray1 = myArray2;
   ^
1 error
raja
Published on 06-Sep-2019 12:26:23
Advertisements