Final keyword traps, final keyword traps

Source: Internet
Author: User

Final keyword traps, final keyword traps


I have always been dumb and thought that the final keyword is immutable. What I did not expect is that this is always true for the basic type.

However, for final objects, you can still modify the objects and member variables in the objects. The address of the current object remains unchanged.

Yesterday I also vowed and was told that I modified the object with final, Which is immutable, the value in it will not be modified (in fact, it can be modified )!!! I am ashamed to think about it. I hereby warn myself not to come to any conclusions without knowing the truth.

For basic types, such as final int x = 1, then x is "real" and immutable, that is, if x = 0, a compilation error is displayed. (There are no other objects in it .)

For non-basic types, such as final Date date = new Date (), date. setYear (2015) will not cause any compilation errors. Because the address of this object is not modified. However, if date = null is performed, a compilation error occurs because the date address of the object changes.


In plain words, it is like: Gao fushuai can have N beebot, but there is only one wife. This wife (object) remains the same, but the wife (object) what clothes are worn by other objects and what cosmetics (objects) are changed.

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.