"Java basics of" Java final keyword: Block inheritance and polymorphism

In Java, the class declaration, when the variables and methods can be modified using the keyword final. The final modified data having a characteristic "end state", indicating "end" is meant. Specific provisions are as follows:

  • final modified class can not be inherited.
  • final modification of the method can not be overridden by subclasses.
  • final modified variables (member variables or local variables) becomes constant, it can only be assigned once.
  • final member variables must be modified in a statement that assignment, if there is no assignment at the time of the statement, then the assignment of only one chance, and only explicit assignment in the constructor before you can use.
  • final modified local variables can only be declared without values, and then a one-time assignment.


final general versatility for modifying those functions, data values, or mode can not be arbitrarily changed, in order to avoid misuse, for example, which performs the mathematical triangulation method, a method of exponentiation function and the like, and the mathematical constant π = 3.141593, e = 2.71828 and so on.

In fact, to ensure that the final state, providing a method of the above-described classes and constants java.lang.Math also been defined as final.

Note that, if the reference type (class of any type) variable marked final, then the variable can not point to any other object. But you can change the contents of the object, because the only reference itself is final.

If the variable is marked final, as a result of making it constant. Want to change the value of the final variable will result in a compilation error. Here is an example of a properly defined final variable:

1 public final int MAX_ARRAY_SIZE = 25; // General constant capital

Because of the constant final modification, it can not be inherited.

Consider the following code:

public  final  class Demo {
     public  static  final  int TOTAL_NUMBER =. 5 ;
     public  int ID;
     public Demo () {
         // illegal, for a final evaluation of the secondary variable TOTAL_NUMBER
         // because ++ TOTAL_NUMBER TOTAL_NUMBER + =. 1 corresponds TOTAL_NUMBER 
        ID = ++ TOTAL_NUMBER; 
    } 
    public  static  void main (String [] args) {
         Final Demo T = new new Demo ();
         Final  int I = 10 ;
         Final  int J;
        J20 is = ; 
        J = 30;   // illegal, for a final secondary variable assignment 
    } 
}

final class can also be used to modify (in front of the class keyword), prevented this kind send another unexpected sub-category, such as Java.lang.String is a final class. This is done for security reasons, because to ensure that if there is reference to the string, you must be a class String string, rather than some other type of string (String class may be inherited and malicious tampering).

The method can also be final modification, the modified final method can not be overwritten; final variables can also be modified, the modified final variables change their values are not allowed in the future to create the object. Once a class is declared final, the method comprising the class will also be implicitly declared final, but not a variable.

The final method is modified static binding, no polymorphism (dynamic binding), does not require re-retrieval method table at runtime, the code efficiency can be improved. In Java, modified or private static method is implicitly declared as final, because dynamic binding does not make sense.

Due to the dynamic binding will consume a lot of time and resources is not necessary, so there are some programmers think: unless there is sufficient reason to use polymorphism, or should be modified by all methods are final.

Such knowledge is somewhat extreme, because the JVM run-time compiler capable of real-time information monitoring program, can inherit the exact relationship between the class know. If a method is not covered and it is short, it is possible for the compiler optimization process, this process is referred to as inline (inlining). For example, inline call e.getName () will be replaced by access e.name variables. This is a very significant improvement, due to the CPU while calling methods of instruction, the branch will disrupt the use of strategies prefetch instruction, so this is considered undesirable. However, if getName () are covered in another class, the compiler will not know what to do code coverage will be operating and, therefore, it can not be processed inline.

Reference: https://www.cnblogs.com/Coda/p/4423592.html

 

Guess you like

Origin www.cnblogs.com/jssj/p/11372582.html