The final keyword is used to restrict the user, any field that we
never expect to be changed (be that a primitive value, or a reference to
an object, whether or not that particular object is itself immutable or
not), should generally be declared as a final variable. We are always
allowed to initialize a final variables and the compiler makes sure that
we can do it only once.
Example : Making a collection reference variable final means only reference can not be changed but you can add, remove or change object inside collection.
For example:
private final List loans = new ArrayList();
loans.add(“home loan”); //valid
loans.add("personal loan"); //valid
loans = new Vector(); //not valid
The final keyword helps in making a variable unmodifiable after the initialization. Once the identity of a final object reference is set, it can still change its state, but not its identity (that is, you can not re-point the object reference to some other object).
Note that variable declared using final keyword should not be called as constants. Because when an array is declared as final, then the state of the object stored in the array can be modified. In this case, we need to make it immutable in order not to allow modifications to it.
final keyword can be used in the following contexts.
1) Declaring the Variable as final
If we declare the variable as final, then we cannot change the value of it (It will be constant).
If the final variable that have no value assigned at the time of declaration, then it is called blank final variable or uninitialized final variable and it can be initialized in the constructor only. The blank final variable can also be a static which will be initialized only in the static block.
Example :-
2) Declaring the Parameter as final
If we declare any parameter as final, then we cannot change the value of it.
Example :-
3) Declaring the Method as final
If we declare any method as final, then we cannot override it.
Example :-
4) Declaring the Class as final
If we declare any class as final, then we cannot extend it, means we can use the class as a Base Class Functionality.
Example :-
Example : Making a collection reference variable final means only reference can not be changed but you can add, remove or change object inside collection.
For example:
private final List loans = new ArrayList();
loans.add(“home loan”); //valid
loans.add("personal loan"); //valid
loans = new Vector(); //not valid
The final keyword helps in making a variable unmodifiable after the initialization. Once the identity of a final object reference is set, it can still change its state, but not its identity (that is, you can not re-point the object reference to some other object).
Note that variable declared using final keyword should not be called as constants. Because when an array is declared as final, then the state of the object stored in the array can be modified. In this case, we need to make it immutable in order not to allow modifications to it.
final keyword can be used in the following contexts.
1) Declaring the Variable as final
If we declare the variable as final, then we cannot change the value of it (It will be constant).
If the final variable that have no value assigned at the time of declaration, then it is called blank final variable or uninitialized final variable and it can be initialized in the constructor only. The blank final variable can also be a static which will be initialized only in the static block.
Example :-
2) Declaring the Parameter as final
If we declare any parameter as final, then we cannot change the value of it.
Example :-
3) Declaring the Method as final
If we declare any method as final, then we cannot override it.
Example :-
4) Declaring the Class as final
If we declare any class as final, then we cannot extend it, means we can use the class as a Base Class Functionality.
Example :-
1
2
3
|
final class Student
{
}
For more Info plese visit: https://javarevisited.blogspot.com/2011/12/final-variable-method-class-java.html |
Thank you for posting the valuable information.
ReplyDeleteFull Stack Online Training in Hyderabad