The JAVA keyword final is used to modify data, methods, or classes. It usually means "unchangeable", that is, data cannot be changed, methods cannot be overwritten, and classes cannot be inherited. Final is generally used for two reasons: Design and efficiency. With the upgrade of the JAVA version, some efficiency problems can be handled by the compiler and JVM. Therefore, using final to solve the efficiency issue is not that important.
Most Final modifiers are used in primitive domains or immutable domains (if all method methods in the class do not change their objects, this type is immutable class. String is an immutable class ).
Final data]
There are two main scenarios for Final keywords to modify data:
1. constants during compilation
2. Value initialized during runtime
A compile-time constant refers to a domain that is both final and static (by convention, all compile-time constants are named in uppercase letters and each word is separated by an underscore ), it only occupies a segment of storage space that cannot be changed. The compiler can substitute a compile-time constant into any computing formula that may be used for it. That is to say, it can execute computation statements during compilation, which reduces the load on the runtime. A compile-time constant must be assigned a value (not necessarily a basic type) during definition ).
The value initialized during runtime. For the basic type, final makes its value unchangeable. For object reference, final makes the reference unchangeable, that is, it cannot be changed to another object. However, the object itself can be modified (applicable to arrays, and arrays are also objects ).
Copy codeThe Code is as follows: public class javaFinalData {
Private static final String TESTD = "test ";
Public static final String TESTE = "test ";
Public static final String [] TESTF = {"1", "2"}; // non-basic type
Private static final String [] TESTG = new String [2];
Public static void main (String args []) {
Final int testA = 1;
Final String testB = "test ";
Final int [] testC = {1, 1, 2 ,};
System. out. println (testC [1]);
TestC [1] = 123;
System. out. println (testC [1]);
}
}
[Unassigned final domain]
JAVA allows the generation of unassigned final domains, but must be assigned a value to the final domain at the domain definition or each Constructor (the number of constructor must be assigned several times ), make sure that it is initialized before use. In this way, the final can be used more flexibly. in the same class, different values are assigned according to different objects, but the unchangeable features are maintained.
Copy codeThe Code is as follows: public class javaBlankFinal {
Private final int blank;
Public javaBlankFinal (){
Blank = 2011;
}
Public javaBlankFinal (int temp ){
Blank = 2012;
}
Public javaBlankFinal (String temp ){
Blank = 2014;
}
Public static void main (String args []) {
New javaBlankFinal ();
}
}
[Final method]
There are two reasons for using the final method: first, locking the method to prevent the method from being overwritten, and ensuring that the behavior of the method remains unchanged in the inheritance; and second, converting the method call to inlining ), to reduce the overhead of method calls. However, in the latest version, the JVM can be optimized on its own, so you do not need to use the final method to handle efficiency issues.
Note that all private methods in the class are implicitly specified as final methods (you can also add final modification to them, but it is meaningless ). When you try to override a private method, the compiler does not report an error. However, you actually did not overwrite the method, but only generated a new method. Because the private method cannot be accessed by external classes, it cannot be overwritten.
The @ Override annotation can prevent the above problems. As shown in the program:
Copy codeThe Code is as follows: class finalFunction {
Private void finalFunctionA (){
System. out. println ("finalFunctionA ");
}
Private final void finalFunctionB (){
System. out. println ("finalFunctionB ");
}
Final void finalFunctionC (){
System. out. println ("finalFunctionC ");
}
Void functionD (){}
}
Class overrideFinalFunction extends finalFunction {
// @ Override add @ Override annotation to identify whether it is override
Public void finalFunctionA (){
System. out. println ("override finalFunctionA ");
}
Public final void finalFunctionB (){
System. out. println ("override finalFunctionB ");
}
// Final void finalFunctionC () {}// Cannot override the final method from finalFunction
@ Override
Void functionD () {}// real override method
}
Public class javaFinalFunction extends finalFunction {
Public static void main (String args []) {
FinalFunction ff = new finalFunction ();
// Ff. finalFunctionA (); // The private method cannot be called.
// Ff. finalFunctionB ();
OverrideFinalFunction off = new overrideFinalFunction ();
Off. finalFunctionA (); // public Method
Off. finalFunctionB ();
}
}
[Final class]
The final class is generally used for design reasons and cannot be inherited. This ensures that the behavior of the class will not change, and may avoid some security crises. All methods in the Final class are implicitly specified as the final method, so they cannot be overwritten (because the final class prohibits inheritance, it cannot overwrite the methods in its class ). There are many examples of final applications in the Java core API, such as java. lang. String. Specify final for the String class to prevent overwrite length () and other methods.
For final fields, even if a class is declared as final, the fields in the class will not automatically become final fields.
Copy codeThe Code is as follows: final class finalClass {
Int testA = 2011;
}
// Class extendFinalClassextends finalClass {}// can not extendthe final class finalClass
Public class javaFinalClass {
Public static void main (String args []) {
FinalClass fc = new finalClass ();
System. out. println (fc. testA );
Fc. testA = 2012;
System. out. println (fc. testA );
}
}