Java Learning Note (23): Final keyword

Source: Internet
Author: User

There are three usage scenarios for the final keyword:

  • Final Modifier class
  • Final Modification method
  • Final modifier variable

The final decorated class, which can no longer inherit from a subclass.

A class can be designed as a final class as long as the following conditions are met:

  1. is not a class specifically designed for inheritance, and it is certain that the class will not be extended
  2. For security reasons, do not want to be modified again

The final modified method can be called by the quilt class, but cannot be overridden by a quilt class method.

The following conditions are met to design a method as the final method:

  1. You do not want the subclass method to overwrite the methods of the parent class.
  2. Method called in the constructor (initialization method), typically using the final keyword

A final modified variable can only be assigned once and can no longer be assigned once it has been assigned an initial value.

Commonly used to set global static variables: public static final modified variable (word authoritative capitalization), directly using the class name can be called.

Java Learning Note (23): Final keyword

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.