The definition of final variables is not complex, that is, once initialized, variables cannot point to other objects. In C ++, they are a const pointer, and
It is not a pointer to the const variable. The const pointer means that it can only always point to the address at the time of initialization, but the object in that address itself
The pointer to the const variable indicates that the object itself cannot be modified.
For example, final stringbuffer sb = new stringbuffer ("axman ");
SB = new stringbuffer ("Sager"); // error. SB cannot point to other objects.
SB. append ("was changed! "); // The earth object to which sb points can be modified.
Let's first talk about final variable initialization:
Many articles have said this: the initialization can be in two places: one is its definition, and the other is in the constructor. Either of them can be selected.
Nonsense!
Final variables can be initialized at any place where they can be initialized, but can only be initialized once. Once initialized, they cannot be assigned again.
Value (re-pointing to other objects), must be explicitly initialized as a member variable, while as a temporary variable, you can only define and do not initialize (of course, cannot reference)
Even as a member variable in a class, it can also be initialized in the initialization block, so "the initialization can be in two places. One is its definition,
Second, in the constructor, either of the two can be "Incorrect.
As a member variable, the final field can be designed as a non-variable class, which is a required condition but not a sufficient condition. At least it can ensure that the field is not
It will be changed in the form of setxxx (), but it cannot be ensured that the field itself is not modified (unless the field itself is also a constant class );
For final variables of method parameters:
If the variables of method parameters are defined as final, more than 90% of the articles say, "when you do not need to change the object variables used as parameters in methods, make it clear
Using final statements will prevent you from accidentally modifying the variables outside the calling method. "
Nonsense!
I don't know whether this modification refers to re-assigning the value or modifying the object itself, but in either case, the above statement is wrong.
If the value is re-assigned, then:
Public static void test (INT [] X ){
X = new int [] {1, 2, 3 };
}
Int [] out = new int [] {4, 5, 6 };
Test (out );
System. Out. println (out [0]);
System. Out. println (out [1]);
System. Out. println (out [2]);
Call test (out); in any case, it will not affect the out variable. It doesn't make sense if you add final. Final will only force the Method
Declare one more variable name, that is, change x = new int [] {1, 2, 3}; to int y = new int [] {1, 2, 3 }; nothing else actually makes sense.
If it is to modify the object itself:
Public static void test (final int [] X ){
X [0] = 100;
}
Int [] out = new int [] {4, 5, 6 };
Test (out );
System. Out. println (out [0]);
Can't you modify the final modification? Therefore, the final in the method parameters is used to avoid affecting the variables outside the called method.
So why should we add final to the parameter? In fact, adding final to method parameters is the same as adding final to method variables, that is,
Ensure the call consistency when passed to the internal class:
Abstract class absclass {
Public abstract void M ();
}
Now, if I want to implement an anonymous call to absclass in a method, we should:
Public static void test (final string s ){
// Or final string S = "axman ";
Absclass c = new absclass (){
Public void M (){
Int x = S. hashcode ();
System. Out. println (X );
}
};
// Other code.
}
In fact, this code is compiled:
Public static void test (final string s ){
// Or final string S = "axman ";
Absclass c = new absclass (s ){
Private final string S;
Public absclass (string s ){
This. S = s;
}
Public void M (){
Int x = S. hashcode ();
System. Out. println (X );
}
};
// Other code.
}
That is, the variable of the external class is passed to the private member of the internal class as the parameter of the constructor.
If there is no final, then:
Public static void test (string s ){
// Or string S = "axman ";
Absclass c = new absclass (){
Public void M (){
S = "other ";
}
};
System. Out. println (s );
}
It will be translated:
Public static void test (string s ){
// Or string S = "axman ";
Absclass c = new absclass (s ){
Private final string S;
Public absclass (string s ){
This. S = s;
}
Public void M (){
S = "other ";
}
};
System. Out. println (s );
}
The S of the internal class points to "other" again and does not affect the test parameter or the S defined by the External. What you see
Public static void test (string s ){
// Or string S = "axman ";
Absclass c = new absclass (){
Public void M (){
S = "other ";
}
};
System. Out. println (s );
}
The syntax is a second, which is changed in the internal class, but the result is printed and you think it is the same s, but it is still the original "axman ",
Can you receive such results?
Therefore, final restricts the consistency of two different variables in syntax.