Strictly speaking, the final modified variable cannot be changed, and once the initial value is obtained, the value of the final variable cannot be re-assigned, so there is a difference between the final decorated member variable and the decorated local variable.
1. Final member variable
General member variables are initialized with class initialization or object initialization. When the class is initialized, the system allocates memory for the class field of the classes and assigns default values. That is, you can assign an initial value to a class field when a static initialization block is executed, and an instance field can be assigned an initial value when a normal initialization block or constructor is executed. Therefore, the initial value of a member variable can specify a default value when the variable is defined, or you can specify an initial value in the initialization block, constructor. If you do not assign an initial value to a class member variable in each of these cases, then the system will give the final decorated class member variable, one of the default values, such as: 0, ' \u0000 ' \, false, or null. (At this point the final member variable loses the meaning of existence).
To sum up, there are a few issues to be aware of when using final member variables:
static field: The initial value must be specified in the static initialization block or when declaring the field
Normal field: You must specify an initial value in a non-static initialization block, declaring the field or constructor
Final decorated class member, the programmer is best to specify its initial value
2. Final local variables
The system does not initialize local variables, and local variables must be initialized by the programmer's display. So when you use final to decorate a local variable, you can either specify a default value when you define it, or you can specify no default value. But it can only be initialized once.
3. Final decorated basic type variable and reference type variable difference
1). When final modifies the base variable type, the underlying type variable cannot be re-assigned, so the underlying type variable cannot be changed
2). When the final modifier references a type variable, final only guarantees that the address referenced by the reference type variable will not change, that is, the same object is referenced all the time, but that the object (the value of the object's non-final member variable can be changed) can change completely
4. The final variable that can perform "macro substitution"
For a final variable, whether it is a class field, an instance field, or a local variable, as long as the variable satisfies 3 conditions, the final variable is no longer a variable, but rather a direct amount.
1). Decorate with the final modifier
2). An initial value was specified when the field variable was defined
3). This initial value can be determined at compile time
5. Final-Modified methods cannot be overridden, and final-modified classes may not have subclasses
Learning Resources
http://blog.csdn.net/u010761376/article/details/50564415
Java Creating immutable Objects-final Keyword Usage Summary