The final keyword in Java is used to restrict the behavior of the user, in other words, to restrict our programmers. Final can be used to modify: variables, methods, classes.
1) Java final variable
When final is used to modify a variable, the value of the variable cannot be changed, but it becomes a constant, equivalent to the constant keyword in C + +. Take a taste of chestnuts:
1 classbike9{2 Final intspeedlimit=90;//Final Variable3 voidrun () {4speedlimit=400; 5 } 6 Public Static voidMain (String args[]) {7Bike9 obj=NewBike9 (); 8 Obj.run (); 9 } Ten}//End of Class
Output:compile Time Error
The speedlimit variable in this code is final decorated and attempts to modify the variable in the run () method, so the compiler gets an error because the final modified variable can only be assigned once, and once assigned, its value can no longer be changed.
2) Java Final method
The method that is final modified cannot be overwritten. Give me a chestnut:
1 classbike{2 Final voidRun () {System.out.println ("running");} 3 } 4 5 classHondaextendsbike{6 voidRun () {System.out.println ("Running Safely with 100kmph");} 7 8 Public Static voidMain (String args[]) {9Honda honda=NewHonda (); Ten Honda.run (); One } A}
Output:compile Time Error
The compiler again, as we wish, the Father tired bike's run () method was final decorated, the subclass Honda tried to overwrite the parent class of the method, compile error.
3) Java Final class
A class that is final decorated cannot be inherited. One more chestnut:
1 Final classbike{}2 3 classHonda1extendsbike{4 voidRun () {System.out.println ("Running Safely with 100kmph");} 5 6 Public Static voidMain (String args[]) {7HONDA1 honda=NewHonda (); 8 Honda.run (); 9 } Ten}
We don't have to look at the results, just like the above.
Q) Can the final method be inherited?
Ans) Yes, subclasses cannot overwrite the final method of the parent class, but can inherit from it. Chestnuts:
1 classbike{2 Final voidRun () {System.out.println ("Running ..."));} 3 } 4 classHonda2extendsbike{5 Public Static voidMain (String args[]) {6 NewHonda2 (). run (); 7 } 8}
Output:running ...
The final run () method of the parent class bike can be called normally in subclass Honda2.
Q) What is a final variable that is not initialized?
ANS) A variable that is not initialized when declaring a final modified variable, also known as the blank final variable.
Consider a scenario in which a field of an object is assigned once when the object is instantiated, and the value of that field is not changed in the future. This time the blank final variable comes in handy.
1 class student{ 2int ID; 3 String name; 4 Final String pan_card_number; 5 ... 6 }
The Pan_card_number field in student is only assigned once when the student instance is created, and will not change in the future.
Q) When do I initialize the blank final variable?
Ans) The blank final variable can only be initialized in the constructor.
Q) What is the static blank final variable?
ANS) A variable that is not initialized at the time of declaration by static and final modification. The static blank final variable can only be initialized in a statically-coded block. Chestnuts:
1 class a{ 2 static final int data;// static blank final variable 3 static {data=50;} 4 public static void main (String args[]) { 5 System.out.println (a.data); 6 } 7 }
Q) What is the final parameter?
Ans) is the final modified parameter, the value of this parameter cannot be changed. Chestnuts:
1 classbike11{2 intCubeFinal intN) { 3n=n+2;//can ' t be changed as N is final4n*n*N; 5 } 6 Public Static voidMain (String args[]) {7Bike11 b=NewBike11 (); 8B.cube (5); 9 } Ten}
In the code above, the cube () method's parameter n is final decorated, and its internal pair n attempts to modify will produce a compilation error.
Q) Can the constructor be declared final?
Ans) cannot, because constructors are never inherited.
About the final keyword in Java