[JAVA] final modifier Field, javafinalfield
I. Introduction
The final modifier can be used to modify variables, methods, and classes. The final variable cannot be changed once it is assigned a value.
Ii. final member variables
Member variables are initialized with class initialization or object initialization. When the class is initialized, memory is allocated to the class variables. When the object is initialized, memory is allocated to the instance variables.
Therefore, the final variable can specify the initial value during the Declaration, or the initial value in the static initialization block. The final modified instance variable can specify the initial value in addition to the declaration, you can also specify the initial value in the constructor or in a common initialization block.
Note that the specified initial value can only be performed in one place. It cannot be specified either at the time of declaration or in the initialization block.
Package fianlFieldCase; public class Test {// instance field, assigned private final String str = "java"; // instance field, private final String str2; // instance field is not assigned at the time of statement, private final int I is not assigned at the time of statement; // class field, private static final int i2 = 2; private static final int i3; {// assign str2 = "normalFinalField" to the instance field in the normal initialization block ";} static {// specify the initial value i3 = 3 for the class field in the static initialization block;} public Test () {// assign an I = 1 value to the instance field in the constructor; // In the constructor Assign a value to str2 that has been assigned a value during initialization. A compilation error is returned. // Str2 = "re ";}}
Note: final-modified member variables must be explicitly initialized. If they are the same as common member variables, the default value is provided by the system, which is immutable, then this variable is meaningless. Therefore, the JAVA syntax specifies that the final modified member variables must be explicitly initialized.
Iii. final local variables
Final-modified local variables can be assigned values during the declaration or after the declaration, but can only be assigned once. Otherwise, the compilation will fail. The final modified parameters are the same. You cannot assign values again because they are assigned values when the method is called.
Package fianlFieldCase; public class FinalField {public static void print (final String str) {// str cannot be re-assigned // str = "android"; System. out. println (str);} public static void main (String [] args) {// The final String s = "java" is initialized during the declaration "; // if the value is re-assigned, local variables that are not modified by // s = "python"; // final are compiled and not initialized by final int I; // You can initialize I = 1; // re-assign a value later, an error is reported // I = 2; print ("Ruby ");}}
Iv. Differences between final modification of basic type variables and reference type variables
When final is used to modify a basic type variable, the basic type variable cannot be assigned a value again, so the basic type variable cannot be changed.
When final modifies a reference type variable, it is only a reference and stores the object address. The address cannot be changed, that is, it cannot point to a new address, however, the value in the object to which it points can be changed.
The final modification methods and classes will be summarized in the follow-up blog!