Java final initial solution

Source: Internet
Author: User

Final

Final classCannot be inherited, No subclass. The methods in the final class are final by default.
Final MethodIt cannot be overwritten by the quilt method.,Can be inherited.
FinalA member variable represents a constant and can only be assigned once., And the value will not change after the value is assigned.
Final cannot be used to modify the constructor.

1. Final class
The final class cannot be inherited. Therefore, the member methods of the final class have no chance to be overwritten. By default, they are all final. When designing a class, if the class does not need a subclass, the implementation details of the class cannot be changed, and it is sure that the class will not be loaded and extended, it is designed as a final class.

 

2. Final Method
If a class does not allow its subclass to overwrite a method, you can declare this method as a final method.

 

 There are two reasons for using the final method:
First, lock the method to prevent any inheritance class from modifying its meaning and implementation.
Second, efficient. The compiler transfers the final method to the embedded mechanism to greatly improve the execution efficiency.

 

3. Final variable (constant)
The final modified member variable is used to represent constants. Once given, the value cannot be changed!

 

4. Final Parameters
When the function parameter is of the final type, you can read and use this parameter, but you cannot change the value of this parameter.

 

Abstract: http://java.chinaitlab.com/base/724785.html

 

 

 

 

 

 

 

 

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.