Scenarion7: Using Final with the reference variable and array.
What is impact of using final with reference variable or an array in java ?
Example 1: after using final also, we can change the element of an array.
package in.technoscience.finalreference;
class TestForFinal {
public static void main(String args[]) {
final int arr[] = { 1, 2, 3, 4, 5 }; // focus on final.
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 10;
System.out.println(arr[i]);
}
}
}
Output: 10 20 30 40 50
Que 2: what is happening here?
1)The array arr is declared as final, but the elements of array are changed without any problem.
2) Arrays are objects and object variables are always references in Java.
3) when we declare an object variable as final, it means that the variable cannot be changed to refer to anything else.
Example2: A simple program where reference variable is final
Example3:impact of final keyword with reference variable
Output: Compiler Error: cannot assign a value to final variable t1
Que 2: what is happening here?
1)The array arr is declared as final, but the elements of array are changed without any problem.
2) Arrays are objects and object variables are always references in Java.
3) when we declare an object variable as final, it means that the variable cannot be changed to refer to anything else.
Example2: A simple program where reference variable is final
package in.technoscience.finalreference;
class Testing {
int wilChange = 20;
private int willChange;
public static void main(String args[]) {
final Testing t = new Testing();
t.willChange = 30;
System.out.println(t.willChange);
}
}
Example3:impact of final keyword with reference variable
package in.technoscience.finalreference;
class TestForFinal2 {
int iWillMakeUSuffer = 20;
public static void main(String args[]) {
final TestForFinal2 t1 = new TestForFinal2();
TestForFinal2 t2 = new TestForFinal2();
// t1 = t2; // compiler will be unhappy
//t1= t2 means t1= new TestForFinal2 and that is not possible due to final
System.out.println(t1.iWillMakeUSuffer);
}
}
Output: Compiler Error: cannot assign a value to final variable t1
In the above code t1 reference variable is pointing to TestForFinal2 and declared as final and t2 is another reference variable pointing to TestForFinal2.
Now in next line when we trying to refer final variable to refer to new type and that will not be possible as t1 is final.
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 array can be modified.
Example 4: impact of using with the array.
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 array can be modified.
Example 4: impact of using with the array.
package in.technoscience.finalreference;
class ExerciseTest {
public static void main(String args[]) {
final int arr1[] = { 1, 2, 3, 4, 5 };
int arr2[] = { 10, 20, 30, 40, 50 };
arr2 = arr1;
//arr1 = arr2; // here compiler will say remove final from arr1.
for (int i = 0; i < arr2.length; i++)
System.out.println(arr2[i]);
}
}
output will be : compile time error.
As array is an object in java and array name treated as reference variable and again we are using final with an array name and that means that can not point to the another array.
No comments:
Post a Comment