Skip to content

Commit 815e002

Browse files
committed
Final Array Concept [File] Added
1 parent 2c4ed31 commit 815e002

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

arr_final.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class arr_final{
2+
// Using the concept of final keyword with default member of the class
3+
int number = 12;
4+
public static void main(String[] a){
5+
6+
// Defining a final array
7+
final int[] arr = {1,2,3,4,5};
8+
System.out.println("Final Array's Elements are : ");
9+
for(int i = 0;i < arr.length;i++){
10+
System.out.print(arr[i] + " ");
11+
}
12+
System.out.println();
13+
14+
arr[3] = 12; // Changing Elemen's value
15+
System.out.println("Elements are changing the value of one element : ");
16+
for(int i = 0;i < arr.length;i++){
17+
System.out.print(arr[i] + " ");
18+
}
19+
System.out.println();
20+
21+
// In this code block we don't have much difference we just have used to final keyword with the concept of Arrays
22+
// Clearing the final keyword with same class object and its members
23+
final arrfinal obj1 = new arrfinal();
24+
obj1.number = 22;
25+
26+
/*
27+
28+
We can't reassign some value to one which is been declared as final
29+
30+
Example :
31+
final arrfinal obj1 = new arrfinal();
32+
arrfinal obj2 = new arrfinal();
33+
34+
obj1 = obj2 // This will cause an error as we can't assign the same value to it
35+
36+
*/
37+
}
38+
}

0 commit comments

Comments
 (0)