Java: final keyword, javafinal keyword

Source: Internet
Author: User

Java: final keyword, javafinal keyword

 

 

Release date:

Final keywords:

 

  • Final: It is equivalent to making the modified variables, methods, or classes "fixed, finalized", so that the modified variables, methods, and classes cannot be changed again [override of inheritance can be restricted ].
  • Final can modify classes, methods, and variables.
  • Final-modified classes cannot be inherited.
  • The final modifier method cannot be overwritten)
  • The final modifier variable is a constant. The final modifier variable must be explicitly initialized and can only be initialized once. [Initialization can be directly declared and assigned values, or assigned values in the initialization code block or constructor, or passing parameters]
  • Final cannot coexist with abstract interface, because the class modified by final cannot be inherited or overwritten.
  • Final modifier reference variable: Internal data can be modified, but the object to which the object is pointed cannot be modified.

 

 

 

 

 

Supplement:
 

Internal class if you want to use local variables,Only local variables modified by final can be accessed:

The lifecycle of a local variable is different from that of an internal class. When a local variable is modified by final, the variable modified by final becomes a data member in the internal class.

(Java adopts a copy local variable (copy local variables) method. That is to say, the local variables defined as final can be copied and used, and the referenced variables can also be used, you cannot assign a value again. This leads to the illusion that you can access local variable (access to local variables). In this case, because you cannot re-assign values, unexpected events will not occur.)

 

 

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.