Four. Java inheritance and polymorphic 8.Java final keywords: block inheritance and polymorphism

Source: Internet
Author: User
Tags mathematical constants

In Java, when declaring classes, variables, and methods, you can use the keyword final to decorate. Final modified data has "end state" characteristics, which means "final" meaning. Specific provisions are as follows:

    • The final decorated class cannot be inherited.
    • The final decorated method cannot be overridden by a quilt class.
    • A final modified variable (a member variable or a local variable) becomes a constant and can be assigned only once.
    • A final-modified member variable must be assigned at the same time as the declaration, and if it is not assigned at the time of the Declaration, then only the opportunity to assign a value once, and only be explicitly assigned in the constructor method, can be used.
    • A final-modified local variable can only declare a value that is not assigned, and then perform a one-time assignment.


Final is used to modify the generality of the function, implementation or value can not be arbitrarily changed data to avoid misuse, such as the implementation of the mathematical triangle method, power operation and other functions of the method, as well as mathematical constants π=3.141593, e=2.71828 and so on.

In fact, to ensure the final state, the Java.lang.Math class that provides the above methods and constants has also been defined as final.

It is important to note that if you mark a variable of a reference type (the type of any class) as final, the variable cannot point to any other object. But you can change the object's content, because only the reference itself is final.

If the variable is marked as final, the result is to make it a constant. Changing the value of the final variable results in a compilation error. The following is an example of a properly defined final variable:

    1. Public final int max_array_size = ; //Constant name General capitalization

Constants cannot be inherited because they have a final decoration.

Take a look at the following code:

  1. Public Final class Demo{
  2. Public static final int Total_number = 5;
  3. public int ID;
  4. Public Demo() {
  5. Illegal, the final variable total_number is assigned two times.
  6. Because ++total_number is the equivalent of total_number=total_number+1
  7. ID = ++total_number www.uuweb.cn ;
  8. }
  9. Public static void main(String[] args) {
  10. Final demo t = New demo();
  11. Final int i = ten;
  12. Final Int J;
  13. J = ;
  14. J = +; //illegal, two assignment to final variable
  15. }
  16. }


Final can also be used to decorate a class (in front of the class keyword) to prevent the class from deriving a subclass, such as Java.lang.String, which is a final class. This is done for security reasons, because to ensure that once a reference to a string is made, it must be a string of class string, not a string of some other class (The string class may be maliciously inherited and tampered with).

Methods can also be final modified, the final modified method cannot be overridden, the variables can also be final modified, the final modified variables will not be allowed to change their values after the object is created. Once a class is declared final, the method contained by the class is implicitly declared final, but the variable is not.

The final modified method is static bound, does not produce polymorphism (dynamic binding), the program does not need to retrieve the method table at run time, can improve the efficiency of code execution. In Java, methods that are modified by static or private are implicitly declared final, because dynamic binding has no meaning.

Because dynamic binding consumes resources and is not necessary in many cases, some programmers think that unless there is sufficient reason to use polymorphism, all methods should be final decorated.

This is a bit of an extreme understanding, because the instant compiler in the JVM can monitor the program's running information in real-time, and can accurately know the inheritance relationship between classes. If a method is not overwritten and is short, the compiler can optimize it for processing, which is called inline (inlining). For example, inline call E.getname () will be replaced with access to the e.name variable. This is a significant improvement because the branch transfer used by the CPU when processing instructions to invoke a method disrupts the policy of the prefetch instruction, so this is considered undesirable. However, if GetName () is overridden in another class, the compiler will not know what the overridden code will do, so it cannot be inline.

Four. Java inheritance and polymorphic 8.Java final keywords: block inheritance and polymorphism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.