- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we assign values to final arrays in Java?
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
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
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
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
- Related Articles
- Final Arrays in Java
- Can we override final methods in Java?
- Can we initialize blank final variable in Java
- Can we inherit a final method in Java?
- Can we declare constructor as final in java?
- Can we declare an interface as final in java?
- Can we declare final variables without initialization in java?
- How do we assign values to variables in Python?
- Can we declare the main () method as final in Java?
- Can we declare the method of an Interface final in java?
- Can we declare an abstract method final or static in java?
- How can we merge two JSON arrays in Java?
- What do you mean MySQL user variables and how can we assign values to\nthem?
- Can we assign a reference to a variable in Python?
- Assigning values to static final variables in java\n
