There are three usage scenarios for the final keyword:
- Final Modifier class
- Final Modification method
- Final modifier variable
The final decorated class, which can no longer inherit from a subclass.
A class can be designed as a final class as long as the following conditions are met:
- is not a class specifically designed for inheritance, and it is certain that the class will not be extended
- For security reasons, do not want to be modified again
The final modified method can be called by the quilt class, but cannot be overridden by a quilt class method.
The following conditions are met to design a method as the final method:
- You do not want the subclass method to overwrite the methods of the parent class.
- Method called in the constructor (initialization method), typically using the final keyword
A final modified variable can only be assigned once and can no longer be assigned once it has been assigned an initial value.
Commonly used to set global static variables: public static final modified variable (word authoritative capitalization), directly using the class name can be called.
Java Learning Note (23): Final keyword