1. Finishing the final of the underlying data member
This is the primary purpose of final, meaning that it is equivalent to the const, C + +, that the member is decorated as a constant, meaning it cannot be modified. As in the Java.lang.Math class, Pi and E are final members, with values of 3.141592653589793 and 2.718281828459045.
2. Final of the reference to the decorated class or object
In Java, we cannot let an object be decorated as final, but only a reference to the object, which means that even if you write public final A = new A (); In fact, the data of a point object can still be modified, and the reference value of a itself cannot be modified, that is, you can no longer re-assign a value to a. The same situation appears in the array, such as public final int[] A = {1, 2, 3, 4, 5}, in fact the value in a is modifiable, which can be written a[0] = 3. It is now understood that the data in the array in Java cannot be modified to be non-modifiable, while C + + can.
3. Final of the modification method
The modification method's final and the const of the decorated member object in C + + are very different. First, the final meaning of the adornment method is not "non-modifiable", but the method cannot be redefined by the inherited member. (Note that what is said here cannot be redefined, not that the subclass must not define a method of the same name, if the parent class's method is a private type, the subclass is allowed to define the method, and here refers to the inability to redefine the method to make the method rewrite the polymorphism can be achieved, such as do not want a = new B (); A.F (); Such an overriding method occurs)
Example:
public class A {
Final method F
Public final void F () {
System.out.println ("Final method F in Class A is called");
}
}
public class B extends A {
Compile Error! The F method of the parent class is the final type and cannot be overridden!
//! public void F () {
//! System.out.println ("Method F in Class B is called");
//! }
}
In addition, when a method is decorated as a final method, it means that the compiler may load the method inline (inline), which means that the compiler does not invoke the method in a way that normally calls a function, but instead directly uses the code within the method to copy it into the original code by a certain modification ( Inserts a method body directly into the call, rather than making a method call. This allows the code to execute faster (because the cost of calling the function is omitted), such as int[] arr = new Int[3] Call Arr.length (), and so on.
Private methods, on the other hand, are implicitly decorated as final by the compiler, which means that private final void F () and private void F () are no different.
4. Final of the modified class
When a class is decorated as final, it has a clear meaning that the class is not allowed to be inherited, that is, the class is "unrepeatable", and any operations that inherit it will end up with a compilation error. This also underscores the reason that Java uses final instead of const as an identifier. (the member variable may not be final, the member method is final)
Example:
Public final class A {
}
Compile Error! A is the final type and cannot be inherited!
!public class B extends a{
//!}
5. Final parameter
Make final modifications to object parameters. The object variable is passed its reference, and is decorated to prevent unintentional changes during invocation.
The use of final in Java