Take a few minutes to learn about final in Java
.
1, the so-called final refers to the final modified properties, classes, methods can not be changed.
2,final the class, which means that the class cannot be inherited,
For example, if Class B is final decorated, final class b{}, so Class A cannot inherit B.
3,final modifier, which means that the method cannot be overridden,
4,final modifies a variable to indicate that the variable is not available to change;
For example: Final int i=3; so i=5 went wrong.
5. Remember the above 4 sentences first, then what to say in depth final.
1. Modify the final of the base data member
This is the main purpose of final, which is equivalent to C + + 's const, that is, the member is decorated as a constant, meaning that it cannot be modified. For example, PI and E in the Java.lang.Math class are final members with a value of 3.141592653589793
and 2.718281828459045.
2. Modifying the final of a reference to a class or object
In Java, we cannot allow an object to be decorated as final, but only to modify the object's reference, which means that even if you write public final A = new A (); In fact, the data of the object that a is pointing to can still be modified and cannot be modified by the reference value of a itself, that is, you can no longer 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 values in a are modifiable, that is, you can write a[0] = 3. As far as we know, the data in the Java array cannot be modified to be unmodified, and C + + can.
3. Final method of modification
The const of the decorated member object in final and C + + of the cosmetic method is very different. First, the final meaning of the cosmetic method is not "not modifiable," but rather that the method cannot be redefined by the inherited member. (Note that what is said here cannot be redefined, not that subclasses must not define a method of the same name. If the method of the parent class is a private type, and the subclass is allowed to define the method, this cannot be redefined to mean that the polymorphism of the method overrides cannot be achieved by rewriting the method, such as not wishing a 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 of Class A is called");
}
}
Public class B extends A {
Compilation 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 modified to the final method, the means that the compiler might load the method inline (inline), which means that the compiler does not invoke the method the way it normally does, but instead simply copy the code within the method to the original code by a certain modification. This allows the code to execute faster (because the overhead of invoking the function is omitted), such as calling Arr.length () in int[] arr = new Int[3].
On the other hand, private methods are implicitly modified by the compiler to final, which means that private final void F () and private void f () are not different.
4. Final of the Cosmetic class
When a class is decorated to final, it has a clear meaning that the class is not allowed to be inherited, that is, the class is "had out", and any operation that inherits it will end in a compilation error. This also underscores the reason why Java uses final instead of const as an identifier.
Example:
Public Final Class A
}
Compilation error. A is a final type and cannot be inherited.
//! Public class B extends a{
//!}