Final literally means "last, final". This meaning is also expressed in Java.
Final can be used to modify variables (including class attributes, object attributes, local variables and formal parameters), methods (including class methods and object methods), and classes.
1. Final modification class:
Final modification class means that this class has already had the "last, final" meaning. Therefore, classes modified with final cannot be inherited, that is, they cannot have their own subclasses.
If the view inherits a class that has been modified with final, an error may occur during compilation.
2. Final modification method:
The final modification method means that this method already has the "last, final" meaning, that is, this method cannot be rewritten (multiple final modification methods can be overloaded).
One thing to note here is that because the prerequisite for overwriting is that the subclass can inherit this method from the parent class, if the final modified method in the parent class also has access control permissions private,
This method cannot be inherited directly from the subclass. Therefore, the same method name and parameters can be defined in the subclass at this time. At this time, there will be no contradiction between rewriting and final.
New methods have been redefined in subclasses.
public class B extends A { public static void main(String[] args) { } public void getName() { }}class A { /** * Because of private modification, this method cannot be inherited in the subclass. Therefore, the getName method in the subclass is redefined, * belongs to the subclass itself, and the compilation is normal*/ private final void getName() { } /* Because of pblic modification, the subclass can inherit this method, resulting in the rewriting of the final method of the parent class, and the compilation error public final void getName() { } */}3. Final modifier variable:
The final modified variable means that this variable has the "last, final" meaning. Once the final variable is defined and initialized for the first time, the final modified variable value cannot be changed.
Here are some issues to be noted:
1. Final modified variables, whether they are class attributes, object attributes, formal parameters or local variables, these variables need to be displayed initialized (that is, specify the initial value for their display).
For the formal parameters modified by final, it is easy to understand because they are passed on by actual parameters.
For local variables modified by final, the same as variables that are not modified by final, they need to display initialization. That is, local variables need to be displayed initialized.
For general class attributes and object attributes, it can be seen from the initialization process of class and object that default initialization is first performed. Then the variables with displayed assignments are initialized again.
However, for the class attributes and object attributes modified by final, if the initialization is not displayed, the default value will be the default initialization value, which contradicts the starting point of final itself. Therefore, the Java syntax stipulates:
The class attributes and variable attributes modified by final must be displayed and initialized assigned.
In addition, whether for basic data types or reference data types, the final modified variables are displayed for the first time and cannot be modified after initialization. It's easy to understand for basic data types. For reference
Data type, reference variables point to the actual object, but they store the address of the object pointed to. Therefore, its value cannot be modified does not mean that the object pointed to cannot be modified.
4. "Macro replacement"/"macro variable" problems caused by final modification
In Java, macro variables/macro replacement refers to the fact that in Java code, some variables can be directly replaced by their own values during the compilation period and compiled into the .class file. Therefore, this change no longer exists in the compiled .class file.
In variables of type String, sometimes the following situations are often encountered:
public class Test { public static void main(String[] args) { String country = "China"; String name = "qqyumidi"; String userInfo = country + name; //After compilation, the country and name variables are still here, String user = "China" + "qqyumidi"; //After compilation, it becomes Chinaqqyumidi directly }}In line 9 of the above code, the compiled variable result is directly Chinaqqyumidi. In line 8, since country and name are ordinary variables, the compiler cannot directly determine the value of userInfo at compile time, so
The result after compilation here is not directly Chinaqqyumidi.
However, if you want the code in line 8 to be represented directly as Chinaqqyumidi during the compilation period, you need to use the final modifier to modify the country and name variables.
Reason: The variable modified by the final modifier can directly determine its value during the compilation period due to its own characteristics, and this value is unchangeable. During the compilation process, its variables can be directly converted into their values for representation.
public class Test { public static void main(String[] args) { final String country = "China"; final String name = "qqyumidi"; String userInfo = country + name; //After compilation, it is directly Chianqqyumidi }}The above is the full content of the summary of Java final modifier knowledge points (must-read article) brought to you by the editor. I hope it will be helpful to everyone and support Wulin.com more~