Final keyword in Java detailed introduction _java

Source: Internet
Author: User

final Variable
If the final keyword is added before a variable, the variable cannot be changed once it is initialized.

If a final variable is a class member variable, it must be initialized and can only be initialized once.

The parameter in the method can also be a final variable. This is useful when we need to pass a reference-type variable, because sometimes we don't want to invoke the function to modify the variable to affect the value of the object in the original function. So setting a reference variable to the final type can be a valid way for the variable to be invoked to modify the parameter. You can use this variable only in the calling method, but you cannot make any modifications to it.

Copy Code code as follows:

void Test (final int a) {
Can not modify a

}

Final method

If a method in a class is final, subclasses of that class can use this method directly, but you cannot override this method.


Some compilers call the final method directly, inserting the body of the final method into the call to increase efficiency, rather than using conventional methods such as saving breakpoints and pressing stacks.


Final Class

If a class is final, then it cannot be inherited. So the final class is a leaf class and it cannot be abstract. The method in the final class is definitely final (but it does not need to be explicitly added to the final keyword in the method, and of course it doesn't matter) that the final class variable can be final or not final.

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.