1. Final modification
Classes modified by final cannot be inherited, so the member methods of final class cannot be overwritten, and classes modified by final keywords do not have subclasses, so the implementation details of the class cannot be changed and cannot be expanded. All member methods in the final class will be implicitly specified as final methods, and the member variables in the final class can be set to final as needed.
2. Final modification method
If a method in a class is modified by the final keyword, its subclass cannot overwrite the method and can only be inherited by the subclass. If a method in the parent class does not want to be overwritten by its subclass, the method can be defined as the final type. In addition, the private method in the parent class (i.e., the method modified by the private keyword) is not allowed to be overwritten by the subclass. Therefore, the method modified by the private keyword in the parent class is default to the final type. The method modified by the final keyword has the following characteristics:
This method is locked, preventing any inheritance class from modifying its meaning and implementation;
Efficient. When the compiler encounters calling the final method, it will enter the embedded mechanism, greatly improving execution efficiency;
In addition, static methods that use final modification cannot be hidden in subclasses of their class to which they belong;
3. Final modification variables
The meaning of the keyword final is "this is irreplaceable" or "final state";
So why stop the change?
The inventor of the java language may prevent changes due to two purposes:
A. Efficiency issues:
Certain methods of certain classes in jdk are not allowed to be overwritten by users. The designer may think that the method used is the best method.
If the user covers it privately, or covers it due to negligence, it will affect the system capabilities of the JVM or system;
B. Design requirements:
As we all know, some cases must use final keywords, such as the parameter passing of anonymous inner classes in the method.
Some people say that final variables are immutable after assignment.
So what exactly does this unchange mean?
This invariant refers to a reference, an address, and the content of the referenced object is still mutable.
That is to say, this final variable always points to an object, is a constant pointer, not a pointer to a constant.
(1) The basic data type modified by the final keyword cannot be changed once it is initialized;
(2) If it is a variable of reference type, it cannot be allowed to point to another object after initializing it, but the content of the object pointed to by the reference can change. The reason is: the address of the reference object stored in the heap memory of the reference data type. After the final modifys the reference type, the heap memory space pointed to by the reference (or the heap memory address stored in the reference) cannot be changed.
When using final to modify a non-static member variable of a class, there are two ways to initialize member variables:
(3) Initialize during declaration (4) Initialize when declaring a variable. The variable is called blank final, that is, final blank, but the initial value must be assigned to this variable in all constructors of the class where the variable is located.
When using final to modify the static member variable of a class, there are two ways to initialize the static member variable:
(5) Initialize when declaring (6) Initialize in static initialization block When final modifying static variables of the interface, there is only one way to initialize:
(7) Initialize when declaring. When using final to modify local member variables, you only need to ensure that the assignment is initialized before use.
In short, variables modified by the final keyword can not be initialized when declared, but it must be ensured that the variable is initialized before use. Once initialized and assigned, it cannot be reassigned. Try to analyze the output results of the program:
public class Test { public static void main(String[] args) { String a = "hello2"; final String b = "hello"; String d = "hello"; String c = b + 2; String e = d + 2; System.out.println((a == c)); System.out.println((a == e)); }} 4. Final modification parameters
When the method parameter is final type, you can read and use this parameter, but you cannot change the value of the parameter. This variable is created when the method is called and is initialized to the corresponding parameter value. Its value cannot be changed before the method body is executed. The parameter value of the basic data type modified with the final keyword cannot be changed. The reference data type modified with the final keyword cannot be the reference pointing to another object or assigning a value to null again, but the content of the reference object can be changed using this reference. The purpose of final to modify parameters is not to prevent the operation of the parameter from changing the value of the corresponding variable outside the method within the called method, but to prevent the reassignment operation of the parameter within the method, affecting the initial value when the parameter is passed. Moreover, modifying parameters in the method with the final keyword cannot prevent the content of the value of the parameter passed from outside the method from being changed. For details, please refer to the explanation of final modification reference data type.