Android Course---final keywords

Source: Internet
Author: User

When final declares properties, methods, and classes in Java, it can be decorated with the keyword final. The final variable is a constant, which can only be assigned once; The final method cannot be overridden by the quilt class; The final class cannot be inherited. 1. Final variableDeclaring a final field helps the optimizer make better optimization decisions, because if the compiler knows that the value of the field does not change, it can safely cache the value in the register.    The final field also provides an additional level of security by having the compiler force the field to be read-only. Its initialization can be in two places, one is its definition, that is, when the final variable is defined directly assigned to it, and the second is in the constructor.    These two places can only be selected, either in the definition of the value, or in the constructor to give a value, not both in the definition of the value, but also in the constructor to give another value.    Once initialized, the immutable meaning is immutable for the base type, and its reference to the object variable cannot be changed.    When the function parameter is the final type, you can read it using the parameter, but you cannot change the value of the parameter. In addition, when an inner class in a method is used with a parameter variable in a method, this parameter must also be declared final for use. in Java, the normal variable system is automatically initialized, the numeric variable is automatically initialized to 0, and the remaining type variables are automatically initialized to null. However, a variable of the final type must show initialization, and the method of initialization must be assigned directly at the time of declaration or in the constructor method, and cannot be assigned by calling the function. 2. Final MethodIf a class does not allow its subclasses to overwrite a method, you can declare this method as the final method.    The final method is used for two reasons: first, locking the method, preventing any inherited classes from modifying its meaning and implementation. Second, efficient. When the compiler encounters a call to the final method, it moves into the inline inline mechanism, which greatly improves execution efficiency. Note that all private methods in a class are implicitly final. Because the private method cannot be taken, it cannot be overloaded. 3. Final classThe final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. In the design class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is certain that the class will not be extended, then it is designed as the final class. -----------------particular note: For instance constants that are static and final modified, the instance itself can no longer be changed, but for instance variables of some container types (for example, ArrayList, HashMap), the container variable itself cannot be changed , but you can modify the objects stored in the container, which is a lot of programming.

Android Course---final keywords

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.