Final is literally understood to mean "last, final". This is also true in Java.
Final can be used to modify variables (including class properties, object properties, local variables, and formal parameters), methods (including class methods and Object methods), and classes.
1. Final Cosmetic class:
The final modifier class means that this class is already the "last, final" meaning. Therefore, a class that is final decorated cannot be inherited, that is, it cannot have its own subclass.
If the view inherits from a class that has been final decorated, an error occurs during compilation or.
2. Final Modification Method:
The final-decorated method means that this method is already "final, final," meaning that this method cannot be overridden (multiple final-modified methods can be overloaded).
The point to note here is that because the override is premised on that subclasses can inherit this method from the parent class, if the final-decorated method in the parent class simultaneously accesses the control permission as private,
Will cause the subclass to not inherit directly to this method, so you can define the same method name and parameters in the subclass at this point, and you no longer have the override and final contradictions, but
New methods are redefined in subclasses.
public class B extends A {public
static void Main (string[] args) {
} public
void GetName () {
}
}
Class A {
/**
* is not inherited to this method in subclasses because of private adornments, so the GetName method in subclasses is a redefined,
* A method that belongs to the subclass itself, compiles normally/
private final void GetName () {
}/
* Because of the pblic adornment, subclasses can inherit to this method, causing the final method of the parent class to be overridden, and the compilation error public
final void GetName () {
}
*/
}
3. Final modifier variable:
The final-decorated variable indicates that this variable is the "last, final" meaning. Once the final variable is defined and initialized for the first time, the value of the final modified variable cannot be changed.
Here are a few things to keep in mind:
1. Final modified variables, whether they are class properties, object properties, formal parameters, or local variables, are required for display initialization (that is, to display the specified initial value).
The formal parameters for the final modification are well understood because they are passed by the argument.
For the final-decorated local variable, it is necessary to display initialization, just like a variable that is not final decorated. That is, local variables are required to display initialization.
For general class attributes and object properties, as can be seen in the initialization of classes and objects, the default initialization is done first. The variable that has the display assignment is then displayed for initialization.
However, for the final decorated class and object properties, if initialization is not displayed, it defaults to the default initialization value, which contradicts the final point of view, so the Java syntax provides:
The final decorated class properties and variable properties must be displayed to initialize the assignment.
In addition, the final-decorated variable is not modified for the first time since initialization, regardless of the underlying data type or reference data type. For basic data types, it's well understood. For reference
Data type, the reference variable points to the actual object, but it stores the address of the object to which it is pointing, so its value cannot be modified and does not mean that the object it points to cannot be modified.
4. The "Macro substitution"/"macro variable" issue resulting from final modifier variables
In Java, macro variable/macro substitution refers to the fact that in the Java code, some variables can be replaced directly by their own values, which are compiled into the. class file at compile time. As a result, this change is no longer present in the compiled. class file.
In string-type variables, you sometimes encounter the following situations:
public class Test {public
static void Main (string[] args) {
String country = "";
String name = "Qqyumidi";
String userInfo = country + name; This is still the country and name variable
String user = "Country" + "Qqyumidi" after compilation;
In line 9th of the above code, the result of the compiled variable is directly Chinaqqyumidi. The 8th line of code, because country and name are ordinary variables, the compiler cannot directly determine the value of the userinfo at compile time, so
The results compiled here are not directly chinaqqyumidi.
However, if you want the code in line 8th to be directly represented as Chinaqqyumidi at compile time, you need to decorate the country and name variables with the final modifier.
Reason: A variable decorated by the final modifier can determine its value directly at compile time due to its own characteristics, and this value is immutable. In the compilation process, you can directly convert its variables directly to its value itself to represent.
public class Test {public
static void Main (string[] args) {
final String country = "the";
Final String name = "Qqyumidi";
String userInfo = country + name; This is directly Chianqqyumidi
}} after
compiling
The above is a small series for you to bring the Java final modifier Knowledge points summary (must see) all the content, hope to help everyone, a lot of support cloud Habitat Community ~