When a variable is decorated with the final keyword in Java, is the reference immutable or does the referenced object not change?

Source: Internet
Author: User

When a variable is decorated with the final keyword in Java, is the reference immutable or does the referenced object not change?

The address value of the reference object cannot be changed, and the contents of the object that the reference variable points to can be changed.

The final variable always points to this object, which is a constant pointer, not a pointer to a constant.

For example: Final stringbuffer SBU = new StringBuffer ("abc");

Re-assigning it to a value

SBU = new StringBuffer ("");

A compilation error will occur, and the final modified variable cannot be re-assigned.

Sbu.append ("D");

Can be compiled and passed;

Final: Finally. As a modifier

1, you can modify classes, variables, functions.

2. A class that is final modified cannot be inherited. In order to avoid being inherited, quilt-like replication function.

3, the final modification of the method can not be replicated.

4. The final modified variable is a constant that can be assigned only once, either to modify the member variable or to modify the local variable.

When describing things, the value of some data is fixed, then in order to enhance the reading, all of these values are given a name, which is easy to read.

This value does not need to be changed, so with the final modifier, as a constant: The writing specification for constants all letters are capitalized, and if made up of multiple words, the words are connected through _.

5. When the inner class is defined in a local position in a class, only the local variable that is partially final decorated is accessible.

When a variable is decorated with the final keyword in Java, is the reference immutable or does the referenced object not change?

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.