Final use in Java

Source: Internet
Author: User

1. The 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 no longer be extended, then it is designed as the final class. (When do I use final?)

2. The final method cannot be overridden by the method of the quilt class, but can be inherited. 2, Final method if 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. The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.

3. The final variable (constant) represents a constant with the final modified member variable, which can only be assigned once, and must be given the initial value , and the value cannot be changed after the assignment! There are three final modified variables: Static variables, instance variables, and local variables, representing three types of constants, respectively.

As you can see from the example below, once the initial value is given to the final variable, it cannot be changed. In addition, when the final variable is defined, it can be declared without giving the initial value, which is also called the final blank, and in any case the compiler ensures that the blank final must be initialized before it is used. However, final whitespace provides greater flexibility in the use of the final keyword final, so that the final data members in a class can be implemented differently depending on the object, but with the same characteristics that remain constant.

4. Final parameter when the function parameter is the final type, you can read the parameter, but you cannot change the value of the parameter. Note: The private member method of the parent class is not overwritten (overridden) by the class method, so the private type method defaults to the final type. final cannot be used to modify a construction method .

The plain is that the final modification is only readable and cannot be modified.

If you construct the method with the final modification, such as:

1  Public  classa{2           Public Static classpeople3          {4              Finalpeople () {};5          }6     Public Static voidMain (string[] args) {7People p =Newpeople ();8          }9}

Effect:

Reason: The front said that the private member method of the parent class cannot be overridden by a class method, so the subclass cannot directly access the private method of the parent class, that is, it is owned but not used, so the method of private type is the final class by default Type of. And because the constructor method of the parent class is called by default when the subclass object is created, it makes no sense to call the constructor of the parent class to final.

Final use in Java

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.