Java basics: keyword final

Final is a reserved keyword in Java, which can declare member variables, methods, classes, and local variables. Once you declare the reference as final, you will not be able to change the reference, the compiler will check the code, if you try to initialize the variable again, the compiler will report a compilation error. If you intend to initialize member variables of the final type in the constructor or initialization block, do not access the value of the member variable before the initialization, otherwise the program will report an error.

1. Modified variables  

Any member variables or local variables (variables in methods or code blocks are called local variables) declared as final are called final variables. Final variables are often used together with the static keyword as constants.

When final modifies a variable of a basic data type, it must be given an initial value and cannot be changed. When a reference variable is modified, the reference variable can no longer point to other objects.
When a final modifies a basic data type variable, the initial value is not assigned and the reference variable points to other objects. It will report an error.
When the final modified basic data type variable is changed, it will report an error.

2. Modification method

Final can also declare methods. The final keyword is added in front of the method, which means that this method cannot be overridden by a method of a subclass. If you think that the function of a method is complete enough and the subclass does not need to be changed, you can declare this method as final. Final methods are faster than non-final methods because they are statically bound at compile time and do not need to be dynamically bound at runtime.

3. Modification

Classes modified with final are called final classes. Final classes usually have complete functions, and they cannot be inherited. Many classes in Java are final, such as String, Interger, and other wrapper classes.

Feature: Any references to final keywords cannot be modified!

(1) Modified class: indicates that the class cannot be inherited;
(2) Modified method: indicates that the method cannot be rewritten;
(3) Modified variable: indicates that the variable can only be assigned once and the value cannot be modified (constant)


There are also local variables in final. The
system will not initialize local variables. Local variables must be explicitly initialized by the programmer. Therefore, when using final to modify local variables, you can either set the default value in the definition or not. Specify the default value. If the default value is not set in the definition, you can assign an initial value to the final variable in the following code, but it can only be done once and cannot be repeated. Of course, if the default value is already specified when the variable is defined, it is unnecessary and not allowed to assign a value to the variable in the following code.


The difference between final modified basic type variables and reference type variables
final modified basic type variables has been described above very clearly, then what is the difference between reference type variables? For a reference type variable, it only saves a reference relationship, and final only guarantees that the address referenced by the reference type variable will not change, that is, the same object is always referenced, but this object can be changed completely.
 

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/105212045