Final
1. Decoration class
When a class is decorated with final, it indicates that the class cannot be inherited.
All member methods in the final class are implicitly formulated as the final method.
2. Modification method
There are two reasons to use the final method. The first reason is to lock the method in case any inherited class modifies its meaning; the second reason is efficiency, in the early Java implementation version, the final method is converted into an inline call, but if the method is too large, you may not see any performance gains from inline calls. In the recent Java release, these optimizations are not required for final use.
Note: The private method of the class is implicitly specified as the final method.
3. Modifier variables
The modifier variable is the place where final is used most.
For a final variable, if it is a variable of the base data type, the value cannot be changed once initialized, and if it is a variable of a reference type, it cannot be pointed to another object after it has been initialized.
1) What is the difference between a final variable and a normal variable in a class?
When you use final action on a member variable of a class, the member variable () must be assigned at the time of definition or in the constructor, and once the final variable is initialized, it cannot be assigned again.
Here is an example:
public class test {
public static void main (string[] args) {
String a = "Hello2";
final String b = "hello";
String d = "hello";
String C = b + 2;
String e = d + 2;
System.out.println ((A = = c));
System.out.println ((a = = e));
}
}
/span>
The first comparison results in true, and the second comparison results in false. This is the difference between the final variable and the normal variable, when the final variable is the base data type and the string type, if you can know its exact value during compilation, the compiler will use it as a compiler constant. That is, where the final variable is used, the equivalent of direct access to the constant is not required to be determined at run time. This is a bit like a macro substitution in C language. So in the preceding section of code, because B is final decorated, it is treated as a compiler constant, so where B is used, the variable b is replaced directly with its value. The access to variable D needs to be linked at run time.
2) Final and static
Static acts on member variables to indicate that only one copy is saved, and final is used to guarantee that the variable is immutable. Look at the following example:
PublicClassTest {
public static void main (string[] args) {
MyClass MyClass1 = Span class= "Hljs-keyword" >new MyClass ();
MyClass myClass2 = new MyClass ();
System.out.println (MYCLASS1.I);
System.out.println (MYCLASS1.J);
System.out.println (MYCLASS2.I);
System.out.println (MYCLASS2.J);
}
}
class myclass {
Span class= "Hljs-keyword" >public final double i = Math.random ( );
public static double j = Math.random ();
}
/span>
Running this code will reveal that the two J values are the same for each print, while the value of I is different. From here we can tell the difference between final and static variables.
Analysis of the final Java keyword