Deep Java final Keyword basic usage, attention points, and benefits

Source: Internet
Author: User

This article explains the final basic usage and the inside advantage and disadvantage, original is not easy, reprint please indicate the source
The article is divided into 3 parts, will give you the final keyword on the class, methods and variables of the modification of the role, as well as some of the advantages, for the shortcomings you can refer to the Java Programming Ideas, fourth edition of 156 pages. Final class and its merits the final method and some points to be noted in the final and final arguments and some point we need to pay attention to (next article)

One: final class and its advantages

If we modify the Java class with the final keyword, that class becomes the final class.
Why the final class is used:

1. This class is already a very sophisticated class, and we will not make any changes to it, and we do not want this class to be inherited by anyone to change it.
2. For security reasons, we do not want to subclass.
3. We hope that when the JVM is processing, it no longer cares whether the class is inherited or not, and the JVM does not need additional processing to improve execution efficiency.
4. After defining the class as final, the result is only a ban on inheritance-no more restrictions. However, because it prohibits inheritance, all methods in a final class are defaulted to final. The final method is bound in the compile phase, known as static binding (binding), which is always a compile-time amount that can never be changed. When the JVM is running, it will directly embed the final method directly, and the efficiency will be greatly improved.

Common final classes:
Common wrapper classes, such as String Integer Double Boolean, are immutable classes.

object of final class: immutable class means that its object cannot be changed once it is created. The final class object is read-only and can be securely shared in a multithreaded environment without additional synchronization overhead
Recommended Links:
Why string is immutable http://www.importnew.com/7440.html
How to write an immutable class. Http://www.importnew.com/7535.html

Second: Final method and some small points to be noticed in it

Method is the final method, preceded by the final keyword, which means that the method cannot be overridden by the quilt class. The function of a method is complete enough, and you can declare this method final if you do not need to change the subclass.

why the final method is used:
1. The function of a method is sufficiently complete and the subclass does not need to be changed to declare this method final.
The 2.final method is faster than a non-final method because it is statically bound at compile time and does not need to be dynamically bound at run time.
3. The use of the final method may be due to consideration for two reasons:

First: "Lock" the method to prevent any inherited class from changing its original meaning. When designing a program, you can do this if you want the behavior of a method to remain unchanged during inheritance, and cannot be overwritten or rewritten.
Second: The efficiency of program execution. When a method is final, the compiler can place all calls to that method into an "embedded" call. Whenever the compiler discovers a final method call, ignores the usual code insertion method (which is based on its own judgment) for executing the method invocation mechanism (pressing the argument onto the stack; jumping to the method code and executing it; jumping back; clearing the stack's arguments; and finally processing the return value). Instead, it replaces the method invocation with a copy of the actual code within the method body. Doing so avoids the overhead of the method invocation.

We need to note that if the method is too bulky, the program will become too swollen and may suffer from any performance improvements that are not embedded in the code. Because any ascension is offset by the time spent within the method. The Java compiler automatically detects these situations and is quite "sensible" in deciding whether to embed a final method. However, it is best not to fully believe that the compiler can make all the judgments correctly. Typically, you should consider setting a method to final only if the method has very little code, or if you want to explicitly prohibit the method from being overwritten.


Third: Final data and final arguments and some points we need to be aware of (next article)

Final member variables and arguments refer to the final keyword modification before the member variable and the independent variable.

Note the point:
A 1.final member variable must be initialized at the time of declaration (static code block and normal code block) or initialized in the constructor (constructor), otherwise a compilation error will be reported.
2. Cannot assign value to final variable again.
3. All variables in the anonymous class must be final variables.
4. (A blank final variable (blank final variable), which is not initialized with final variables at the time of declaration, must be initialized in the constructor or called this () initialization. If you do not, the compiler will complain that the final variable (variable name) needs to be initialized. )
Both 5.JVM and Java applications cache final variables to improve performance.
6.final variables can be safely shared in a multithreaded environment without the need for additional synchronization overhead.

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.