Final keyword, javafinal keyword

Source: Internet
Author: User

Final keyword, javafinal keyword

The final keyword is a reserved keyword in Java. Once the reference is declared as the final type, the reference cannot be changed.

I. Modify Variables

The member variables or local variables modified by the final keyword are called constants. It is mainly used in the following two areas:

(1) constants during the compilation period, which can only be basic types and must be initialized at definition.

(2) During running. We hope it can behave differently according to the object, but we do not want it to be changed at the same time. It can be a basic type or a reference type. The content of the basic type is immutable. The reference type is immutable, and the content is variable.

Ii. Modification Method

The final keyword modifier method cannot be overwritten by the quilt class.

3. modifier class

Classes modified by the final keyword cannot be inherited.

Iv. modifier Parameters

The final keyword modifies a parameter, indicating that the parameter cannot be changed and the value of this parameter cannot be changed in the method.

To maintain parameter consistency in an anonymous internal class, if the parameter of the passed method needs to be used in an internal class, it must be declared as final.

Simply put, copy references. In order to avoid changing the reference value, such as being modified by the external class method, internal classes get different values, so final is used to make the reference unchangeable.

Therefore, if you define an anonymous internal class and want it to use an externally defined parameter, the compiler will require that the parameter reference be final.

Reference: http://www.cnblogs.com/chenssy/p/3390871.html

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.