Final Arrays in Java13 May 2025 | 6 min read In Java, final is a keyword. It is a non-access modifier. It means that it restricts modifications of the variable, methods, and classes. It ensures that once an entity is declared as final, it can be assigned and defined once. On the other hand, a reference variable, once declared final, can never be reassigned because it refers to another object that makes usage of the final useless. Here, a point to note is that we are bound not to refer to another object. But in context to the object, data can be changed. It means that we can change the state of the object, not the reference. In Java, an array is treated as an object, and object variables are always reference in Java. Here, the concept of a final array comes into play. In this section, we will discuss the final array in Java with an example and Java program for the same. Final ArrayIn Java, when we declare an array as final, it means that the reference to the array cannot be changed after initialization. In simple words, final applies to the reference, not to the contents of the array. Let’s understand it with an example. Final Array ExampleConsider the following code snippet. Key PointsImmutable Reference: Once a final array is assigned, we cannot point the array variable to another array or null. Mutable Contents: We can update, add, or remove elements in the array (depending on the type of array). This behavior is often used to ensure the integrity of a reference while allowing modifications to the data it holds. Implementation of Final ArrayOutput: 7 2 3 10 5 6 In the above example, we observe that the array is declared as final, but the elements of the array are changed. When we remove the comment at line 10, it gives a compilation error. Output: 8, 11, 14, 17, 19, 21 Let’s discuss some other Java programs related to final array. Program 1: Modifying Elements in a Final ArrayOutput: [Apple, Pineapple, Blueberry, Banana, Cherry] ExplanationThe program demonstrates that declaring an array as final in Java makes its reference immutable, meaning the array cannot be reassigned, but individual elements can still be modified. The program initializes a final String array, modifies some elements, and prints the updated values of the array. Program 2: Passing Final Array to a MethodWe can pass a final array to a method. It also modifies the element of the array. Output: [10, 20, 100, 200, 300, 60, 70] Explanation The program demonstrates the final array when passed as a method parameter in Java, where the reference itself cannot be reassigned, but individual elements can be modified. The modifyArray() method updates the third, fourth, and fifth elements (according to index value) of the array, and this change persists outside the method. Program 3: Multi-Dimensional Final ArraySimilarly, we can declare a multi-dimensional array as final. Output: [[56, 2, 3], [4, 5, 19], [7, 45, 29]] ExplanationThe program demonstrates a final 2D array in Java, where individual elements can be modified, but reassigning the reference to a new array is not allowed. The program initializes a final 2D array matrix, modifies some of its elements, and then prints the updated matrix. Attempting to assign a new array to the matrix would result in a compilation error. Program 4: Final Array of ObjectsWhen using an array of objects, we can modify the object's properties but not the reference. Output: 10, 2 3, 40 ExplanationThe program demonstrates a final array of objects. It ensures that the reference cannot be reassigned but allows modification of its elements. Program 6: Final Array Inside a ConstructorOutput: 10 99 30 ExplanationThe program demonstrates a final array in a constructor that ensures that the reference cannot be reassigned but allows modification of its elements. The DataHolder class has a final array data, initialized through the constructor. The modifyData() method changes the value at a given index, showing that elements can be updated. In main(), an object is created with an integer array, the second element is modified, and the updated array is displayed. Next TopicBinary Tree Java |
Handling key-value-based data is a common requirement in various Java applications. Often, data arrives as Strings or String arrays, and it becomes essential to convert them into Maps for efficient processing. In the same context, Map provide an easy way to access and manipulate data with...
5 min read
Java 8 Vs. Java 11 Java has been used by programmers for generations, however, it has developed a lot through different versions of it. From the launch of Java 1.0 in 1996, it now forms the basis for platform-independent programming. J2SE 1.2 (otherwise called Java 2) introduced...
6 min read
In Java, wait() and notify() are methods provided by the Object class, and they are used for inter-thread communication and synchronization. wait() Method The wait() method is a synchronized method in the Java programming language that causes the current thread to give up the lock on an object...
9 min read
Java collectors play a crucial role in the Stream API, providing a convenient way to transform elements of a stream into various data structures such as lists, Sets, or Maps. In this section, we will explore some of the commonly used collector methods in Java 10. toList()...
4 min read
The Java java.util package contains the AbstractSequentialList class, which offers a fundamental implementation of the List interface in order to reduce the tasks involved in implementing this interface using a "sequential access" data storage (such a linked list). To get rid of every element from a...
3 min read
The conversion of N-ary trees to binary tree serves as a standard computer science operation for reducing hierarchy complexity while maintaining hierarchical structures. An N-ary tree allows each node to have multiple children, making it complex to manage using standard tree structures. To efficiently represent an N-ary...
5 min read
Minecraft is a sandbox video game developed by Mojang Studios. It is written in Java programming language. It is developed by Markus Persson. In May 2009, it was released for personal computers. The Minecraft Java edition is a cross-platform play between Windows, Linux, and macOS. It...
4 min read
A number n is given. Our task is to find out the self-descriptive numbers that are present between 1 to n. Self-Descriptive Numbers A self-descriptive number m, is a number that contains b digits in the base b, where the most significant digit is positioned at 0 and...
5 min read
In Java, Singleton class is a class that controls the object creation. It means the singleton class allows us to create a single object of the class, at a time. It is usually used to control access to resources, such as database connections or sockets. It...
3 min read
Java is a powerful programming language known for its versatility and extensive libraries. When working with arrays, you may often encounter scenarios where you need to calculate the sum of two arrays. Whether you're a beginner or an experienced developer, understanding how to accomplish this task...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India