Java know how much (final keyword): Block inheritance and polymorphism

Source: Internet
Author: User
Tags control characters 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 // constant name General capitalization

Constants cannot be inherited because they have a final decoration.

Take a look at the following code:

1  Public Final classdemo{2      Public Static Final intTotal_number = 5;3      Public intID;4      PublicDemo () {5         //illegal, the final variable total_number is assigned two times.6         //because ++total_number is the equivalent of total_number=total_number+17ID = + +Total_number;8     }9      Public Static voidMain (string[] args) {Ten         FinalDemo T =NewDemo (); One         Final inti = 10; A         Final intJ; -j = 20; -j = 30;//illegal, two assignments to the final variable the     } -}


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.

Series Articles:

Java know how much (1) Language overview

Java know how much (2) virtual machine (JVM) and cross-platform principle

Java know how much (3) employment direction

Java know how much (4) the difference between J2SE, Java EE, J2ME

Java know how much (5) Build Java development environment

Java know how much (6) The first example of a program

Java knows how many (7) classes and objects

Java know how much (8) class library and its organizational structure

Java know how much (9) Import and Java class search path

Java know how much (10) data types and variables

Java know how much (11) data type conversions

Java know how many (12) operators

Java know how much (13) Process Control

Java know how many (14) arrays

Java know how much (15) string

Java know how much (StringBuffer) and Stringbuider

Java know how much (17) emphasize the programming style

Java know how many (18) classes are defined and instantiated

Java know how many (19) access modifiers (access control characters)

Java knows how many (20) variables are scoped

Java know how much (+) This keyword is detailed

Java know how many (22) method overloads

Java know how much (23) the basic run order of classes

Java know how much (24) packaging class, unpacking and packing detailed

Java know how much (25) More about Java package

Java know how much (26) claim rules for source files

Java know how much (27) the concept and implementation of inheritance

Java know how many super keywords

Java know how much (29) Overwrite and reload

Java know how much (30) polymorphic and dynamic binding

Java know how many static keywords and Java static variables and static methods

Java know how much (instanceof)

Java know how much (33) type conversions for polymorphic objects

Java know how much (final keyword): Block inheritance and polymorphism

Java know how much (final keyword): 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.