A. Final value of the variable cannot be changed
B. Final methods cannot be rewritten
C. The final class cannot be inherited
1. Final class
The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, cannot be overridden, and the default is final. In the design class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is certain that the class will not be extended, then it is designed as the final class. 2. Final method (cannot be overridden)
If a class does not allow its subclasses to overwrite a method, you can declare this method as the final method.
Public classTest1 { Public Static voidMain (string[] args) {//TODO automatically generate method stubs} Public voidF1 () {System. out. println ("F1"); } //methods that cannot be overridden by a quilt class PublicFinalvoidF2 () {System. out. println ("F2"); } Public voidF3 () {System. out. println ("f3"); } Private voidf4 () {System. out. println ("f4"); } } Public classTest2 extends Test1 { Public voidF1 () {System. out. println ("Test1 Parent class method F1 is overwritten!"); } Public Static voidMain (string[] args) {Test2 T=NewTest2 (); T.F1 (); T.f2 (); //call the final method inherited from the parent class .T.F3 ();//call a method that inherits from the parent class .//t.f4 ();//call failed, cannot inherit from parent class} }
3. Final variable (constant)
A constant is represented by a final modified member variable, which cannot be changed once the value is given!
There are three final modified variables: Static variables, instance variables, and local variables, representing three types of constants, respectively.
As you can see from the example below, once the initial value is given to the final variable, it cannot be changed.
In addition, when the final variable is defined, it can be declared without giving the initial value, which is also called the final blank, and in any case the compiler ensures that the blank final must be initialized before it is used. However, final whitespace provides greater flexibility in the use of the final keyword final, so that the final data members in a class can be implemented differently depending on the object, but with the same characteristics that remain constant.
Package org.leizhimin; Public classTest3 {PrivateFinal String S ="Final instance variable s"; PrivateFinalintA = -; PublicFinalintB = -; Public StaticFinalintE the; Private StaticFinalintB -; PublicFinalintE//final blank, the initial value must be assigned when initializing the object PublicTEST3 (intx) {E=x; } /** * @param args*/ Public Static voidMain (string[] args) {Test3 T=NewTEST3 (2); //t.a=101; //error, the value of the final variable cannot be changed once it is given//t.b=91;//error, the value of the final variable cannot be changed once it is given//t.c=81;//error, the value of the final variable cannot be changed once it is given//t.d=71;//error, the value of the final variable cannot be changed once it is givenSystem. out. println (T.A); System. out. println (T.B); System. out. println (T.C);//accessing static fields using object mode is not recommendedSystem. out. println (T.D);//accessing static fields using object mode is not recommendedSystem. out. println (TEST3.C); System. out. println (TEST3.D); //System.out.println (TEST3.E);//error because E is final and differs depending on the value of the object.System. out. println (T.E); TEST3 T1=NewTEST3 (3); System. out. println (t1. E);//final blank variable e differs depending on the object } Private voidTest () {System. out. println (NewTEST3 (1). A); System. out. println (TEST3.C); System. out. println (TEST3.D); } Public voidTest2 () {finalintA//final blank, assign value when neededFinalintb =4;//Local constants--final cases for local variablesFinalintC//final blank, has not been assigned. A =3; //a=4; Error, the value has been assigned. //b=2, error, has been assigned value. } }
4. Final parameters
When the function parameter is the final type, you can read it using the parameter, but you cannot change the value of the parameter.
Public class t{ int i=8; void int j) { J=8; // J is the final variable value and cannot be changed }
The final keyword in Java