In programming, we may sometimes expect that some data cannot be changed, and final will be useful at this time. Final is the keyword of java, which indicates "This part cannot be modified ". There are two reasons for not wanting to be changed: efficiency and design. There are three cases of using final: data, methods, and classes.
I. final data
Sometimes it is useful to keep the data constant, which can reduce the burden on the system runtime. For these constant data, I can call it a constant ". The main application of "constant" is as follows:
1. constants during the compilation period cannot be changed.
2. during runtime initialization, we hope it will not be changed.
For a constant during the compilation period, initialization has been completed during the class loading process. Therefore, it cannot be changed after the class is loaded, during compilation, it can be substituted into any computing formula that uses it, that is, it can be executed during compilation. Of course, for constants in the compilation period, only basic types can be used, and initialization must be performed at definition.
Some variables, we hope they can behave differently according to the object, but do not want it to be changed at the same time. At this time, we can use runtime constants. For constants in the runtime, they are both basic data types and reference data types. The content of the basic data type is unchangeable, but the referenced data type is immutable, and the content of the referenced object is variable.
Public class Person {private String name; Person (String name) {this. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name ;}} public class FinalTest {private final String final_01 = chenssy; // compile-time constant, which must be initialized and cannot be changed to private final String final_02; // constructor constant, Which is initialized when an object is instantiated. private static Random random = new Random (); private final int final_03 = random. nextInt (50); // use a random number for initialization // reference public final Person final_04 = new Person (chen_ssy); // final points to the referenced data type FinalTest (String final_02) {this. final_02 = final_02;} public String toString () {return final_01 = + final_01 + final_02 = + final_02 + final_03 = + final_03 + final_04 = + final_04.getName ();} public static void main (String [] args) {System. out. println (------------ create object for the first time ------------); FinalTest final1 = new FinalTest (cm); System. out. println (final1); System. out. println (------------ create object for the second time ------------); FinalTest final = new FinalTest (zj); System. out. println (final); System. out. println (------------ modify the reference object --------------); final2.final _ 04. setName (chenssy); System. out. println (final) ;}------------------ Output: ------------ create object for the first time ------------ final_01 = chenssy final_02 = cm final_03 = 34 bytes = bytes ------------ create object for the second time ------------ bytes = chenssy bytes = zj bytes = 46 bytes = bytes -------------- final_01 = chenssy final_02 = zj final_03 = 46 final_04 = chenssy
Here we will only elaborate on one point: Do not think that some data is final and you can know its value during the compilation period. We will know through final_03, where we will use random numbers for initialization, he must know its value at runtime.
Ii. final Method
All methods marked by final cannot be inherited or changed. Therefore, the first reason for using the final method is that the method is locked to prevent any subclass from modifying it. As for the second reason, efficiency is the problem. The lower person does not have a clear understanding of this problem. I will extract this article from the Internet: In the early implementation of java, if a method is specified as final, that is, the compiler agrees to convert all calls to this method into embedded calls. When the compiler finds a final method call command, it will execute the method call mechanism based on its own careful judgment by skipping the normal call method such as inserting program code (pushing parameters to the stack, jump to the method code for execution, then jump back and clear the parameters in the stack to process the returned value), and replace the method call with a copy of the actual code in the method body. This will eliminate the overhead of method calls. Of course, if a method is very large, your program code will expand, so you may not be able to see the performance improvement brought about by embedding, because the performance will be reduced by the amount of time consumed by the method.
I did not understand this paragraph and copied it. The java cool man can explain it !!
The final method of the parent class cannot be covered by the quilt class. That is to say, the subclass cannot have the same method as the parent class.
Public class Custom extends Person {public void method1 () {System. out. println (Person's method1 ....);} // Cannot override the final method from person: the subclass Cannot overwrite the final method of the parent class // public void method2 () {// System. out. println (Person's method2 ...); //}}Iii. final class
If a class is modified using final, it indicates that the class is the final class, and it does not want or allow others to inherit it. In programming, if it is in security or for other reasons, we do not allow any changes to this class, nor do we want it to have child classes. In this case, we can use final to modify this class.
For final-modified classes, its member variables can be final or non-final. If it is defined as final, the final data rule is also suitable for it. Its method will automatically add final, because the final class cannot be inherited, so this is the default.
Iv. final Parameters
In practical applications, in addition to using final to modify member variables, member methods, and classes, we can also modify parameters. If a parameter is modified by final, this parameter cannot be changed.
If we modify this parameter in The method, The compiler will prompt you: The final local variable I cannot be assigned. It must be blank and not using a compound assignment.
Public class Custom {public void test (final int I) {// I ++; --- the final parameter cannot be changed to System. out. println (I);} public void test (final Person p) {// p = new Person (); -- the final parameter is unchangeable. setName (chenssy );}}
It is very useful in the internal class for modifying parameters with final,In an anonymous internal class, to maintain parameter consistency, if the method parameter needs to be used in the internal class, the parameter must be final. For more information, see http://www.cnblogs.com/chenssy/p/3390871.html.
V. final and static
When final and static are used together, a magical chemical reaction occurs. They can modify member variables and member methods at the same time.
For a member variable, once assigned a value, it cannot be changed. We call it a "global constant ". You can use the class name for direct access.
Member methods cannot be inherited or changed. You can use the class name for direct access.