Java Final keyword clairvoyant

Source: Internet
Author: User

Clairvoyant

The final keyword, which can decorate data, methods, classes.

Some students may be silly to find out, here can quickly understand the final;

Final instance domain:

You can define an instance field as final, and you must initialize the domain when you build the object, that is, you must ensure that the value of the field is set after each constructor is executed, and in subsequent operations, it cannot be modified, for example, You can declare the name field in the Employee class as final, because after the object is built, the value is not modified. That is, there is no SetName method.

Class Employee

{

Private final String name;

}

The final modifier mostly applies a basic Type field, or immutable class, String decoration.

The use of final modification of mutable classes can cause confusion.

Private final Date hiredate; This simply means that the object reference cannot be changed after the object constructor, and does not mean that the HireDate object is a constant, and any method can invoke the object referenced by the HireDate.

Do you understand the mystery?

Do not understand the recommendation of a final introduction in more detail: http://www.cnblogs.com/dolphin0520/p/3736238.html

Java Final keyword clairvoyant

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.