Java final summary and javafinal Summary
The Java keyword final has a final and unchangeable meaning. It can modify non-abstract classes, non-abstract class member methods, and variables.
Error: the class "TestFinal" is either abstract or final, not both.
1. The final modified class is the ultimate class. If it cannot be inherited by other classes, it cannot be a subclass.
2. Because the final class has no subclass, the methods in the final class cannot be overwritten.
All methods and variables are final and do not need to be explicitly declared as final.
Public final class TestFinal {public static final int I = 1; // The data member in the final class can be the final protected int j; // The data member in the final class may not be the final public void f () {}// the method in the final class is final, and public final void g () may not be explicitly declared () {} // methods in the final class can be declared as final, but it makes no sense}
3. The final method cannot be overwritten by the quilt class, but can be inherited.
Public class TestFinal extends FinalOnMethod {public static void main (String [] args) {TestFinal tFinal = new TestFinal (); tFinal. one () ;}} class FinalOnMethod {public final void One () {System. out. println ("hello, I am a final method, but my class is not decorated with final !!!!!!!!!!!!! ");}}
Next we will rewrite the final modified method.
An error is reported during compilation.
Therefore, we think that private methods cannot be inherited.
Because the system treats Private Members as final, we can also express a private member as fianl, but this is meaningless.
4. the final member variable indicates a constant and can only be assigned once. The value is not changed after the value is assigned. If the value is forcibly assigned, a compilation error occurs.
5. final cannot be used to modify the constructor. If a constructor is not inherited, it cannot be overwritten. That is to say, the constructor itself is final and there is no need to use final labels.