Fianl modifier of Java, javafianl Modifier
The variable modified by fianl cannot be changed. Once the initial value is obtained, the final variable value cannot be re-assigned.
1. final member variable (the programmer must explicitly specify the initial value)
Class variable: the initial value must be specified in the static initialization block or when the variable is declared.
Instance variable: You must specify the initial value in a non-static initialization block, declare the variable, or in the constructor.
2. final local variables
The local variables defined by final can be defined by default or not. If the default value is not specified during definition, you can specify the default value in future code, but only once.
3. Differences between final modification of basic type variables and reference type variables
When final modifies a basic type variable, you cannot assign a value to the variable of the basic type object. However, when final modifies a reference type variable, it only saves a reference. final only ensures that the reference address of this application type variable remains unchanged, that is, it always references the same object, but this object can be completely changed.
Public class Test () {public static void main (String [] args) {// final modifier array variable, is a reference variable final int [] iArr = {5, 6, 7, 8 }; // output 5 6 7 8 System. out. println (Arrays. toString (iArr); // assign values to the array elements, valid iArr [2] =-8; // output 5-8 7 8 System. out. println (Arrays. toString (iArr ));}}
4. final Method
The final modifier method cannot be overwritten.
5. final class
The classes modified by final cannot have child classes and cannot be inherited.