Final keyword detailed

Source: Internet
Author: User

The final keyword's role I think we should be very clear, can recite backwards.

1.final properties are immutable

2.final method is not covered

The 3.final class is not inheritable

This blog is to further elaborate the final keyword.

First of all, the final attribute, the final attribute is divided into member properties and static properties. We know that the final attribute must be initialized and can no longer be assigned after it is initialized, but the display initialization can be done in three places, defined, in the constructor, in the initialization block. Note that initialization cannot be done in a method, and if it is not assigned in three places above, the property is given a default value and cannot be changed again, so this time there will be a compilation error if the assignment is initialized in the method. In other words, when the final attribute is declared, it can not be directly assigned, in these three places can also be assigned value, the effect and declaration when the direct assignment is the same. Why, then? You can use the Javap-c class name (where the class name does not add the. class suffix) to look at the bytecode. Found in these three places, in fact, the equivalent of all in the construction method to assign values. In this way, I think we can understand the reasons for the above. Static properties are similar to normal properties, but when initialized, they can only be assigned at the time of Declaration and in a static code block, and the final static property is actually assigned in the static code block when it is initialized with JAVAP view of the byte code. In addition, the final variable does not take the default initial value, must be shown in the above three places to display initialization, not in addition to the above three places to assign values, or compile the error. Therefore, the final variable is not the value of the variable can not be changed so simple.

After the above understanding, again in Java, "macro replacement" concept. Macro substitution was originally a concept in C, which is used in the C language for constant substitution. The same is true in Java. The usage is that we declare an attribute variable, the final variable is a macro variable, whether static or Non-static, with the final keyword, and when initialized with a direct value, where initialization can only be initialized at declaration time, not a variable or an expression. The main feature of macro variables is that the value will be determined at compile time, in fact, to understand the simple point, the macro variable is like a placeholder, where the variable is called, at compile time, the variable is replaced with a value. If you initialize with a variable or an expression, you can assign a value only at run time.

The final method cannot be overridden, and here it is emphasized that you should not add private to the final method, although there is no syntax error, but this is meaningless. In addition, when a subclass has a method that overrides the parent class, the compilation fails to make an error even if no @override annotation is added. This can also occur if you want to overwrite a parent class method that cannot be accessed in the same subclass.

Finally, let's say the final local variable. If the inner class is used in the method and the inner class invokes the local variable in the method, then the local variable needs to be declared final, and if it is not declared, the compile will make an error. Cause: First, when the local variable is declared final, the life cycle of the local variable is longer than the method in which it is used, because it is possible for the internal class to have a life cycle that is longer than the method, to form a closure, and to be final in order for the program to be healthy. Finally, the final local variable, when declared, does not take the default initial value and does not have to be initialized at the time of declaration, and can be assigned later, but it must be assigned before it can be used, or it will be an error at compile time. And the final local variable cannot be accessed outside the method.

Through the above can be summed up, final variables must be initialized and must be initialized at specified places, such as final static variables that can be initialized only at declaration time and in static code blocks, when initialization is done elsewhere, and final local variables can be initialized anywhere in the method. After initialization, it can no longer be assigned a value.

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.