Final keyword
Final can be decorated with classes, methods, and variables.
The final decorated class cannot be inherited
The final modified method cannot be overridden
The final modified variable is a constant. Can only be assigned once.
An inner class can only access a final decorated local variable.
Class Fu{void Method () {//called the underlying system's resources}}class Zi extends Fu{void method () {System.out.println ("haha");}}
From the above can be seen that inheritance is a disadvantage, is to break the encapsulation.
At this point we can use the final keyword to not let others inherit, as follows:
Final class Fu{void method () {//called the underlying system's resources}}class Zi extends Fu{void method () {System.out.println ("haha");}} This will be the error when compiling.
Final initialization
Final initialization is not a fixed initialization, but an explicit initialization.
int x; When this is written, X is initialized to 0.
But if it is
final int x; This will cause an error in compiling. Because it does not support fixed initialization
Normally there will be a static modifier for final
static final int x = 8;
Because by adding final, it means that he will be shared.
Why use final modifier variables. In fact, if a data is fixed in the program,
So it is possible to use this data directly, but the reading is poor, so it gives the data a name.
And the value of this variable name cannot be changed, so add final fixed.
Specification: Constant All letters are capitalized, multiple words, in the middle with _ connection.
Java Learning Personal Memo keyword final