Java Learning Personal Memo keyword final

Source: Internet
Author: User

Final keyword
Final can be decorated with classes, methods, and variables.
The final decorated class cannot be inherited
The final modified method cannot be overridden
The final modified variable is a constant. Can only be assigned once.
An inner class can only access a final decorated local variable.

Class Fu{void Method () {//called the underlying system's resources}}class Zi extends Fu{void method () {System.out.println ("haha");}}

From the above can be seen that inheritance is a disadvantage, is to break the encapsulation.
At this point we can use the final keyword to not let others inherit, as follows:

Final class Fu{void method () {//called the underlying system's resources}}class Zi extends Fu{void method () {System.out.println ("haha");}} This will be the error when compiling.



Final initialization
Final initialization is not a fixed initialization, but an explicit initialization.
int x; When this is written, X is initialized to 0.
But if it is
final int x; This will cause an error in compiling. Because it does not support fixed initialization

Normally there will be a static modifier for final
static final int x = 8;
Because by adding final, it means that he will be shared.

Why use final modifier variables. In fact, if a data is fixed in the program,
So it is possible to use this data directly, but the reading is poor, so it gives the data a name.
And the value of this variable name cannot be changed, so add final fixed.

Specification: Constant All letters are capitalized, multiple words, in the middle with _ connection.

Java Learning Personal Memo keyword 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.