Sixth chapter, Class regeneration

Source: Internet
Author: User

6.0.1 Initialize the base class:

Initialization Knowledge point 1: A demonstration of this class of Art,drawing,cartoon Three program examples shows that the build is done on the "outside" of the underlying class, so the underlying class is initialized correctly before the derived class accesses it. Even if you do not create a builder for cartoon (), the compiler will automatically synthesize a default builder for us. and emits a call to the underlying class builder. But note that the default constructor is automatically invoked here, and you cannot use this method if you have a custom parameter inside the constructor, and you must use Super (XX) to display the calling base class constructor if there is no default constructor.

6.8.1 Final Data

Many programming languages have their own way of telling the compiler about a "constant" of data. The constants are mainly used in the following two areas:

⑴. Compile-time constants, which will never change.

⑵. A value initialized at runtime, we do not want it to change.

For compiler constants, the compiler (program) can "encapsulate" the constants into the desired computation. That is, the calculations can be executed in advance between the compilers, saving some overhead at run time. In Java, these forms of constants must belong to the basic data type (primitives) and are expressed in the final keyword. When you define such a constant, you must give a value.

Both static and final fields can store only one data and must not be changed.

If you use final instead of the underlying data type with the object handle, it's a bit confusing. For a basic data type, final will change the value to a constant, but for an object handle, final will turn the handle into a constant. When making a declaration, you must save the handle from initializing to a specific object. And you can never turn a handle into another object. However, the object itself can be modified. Java does not provide any means to turn an object directly into a constant (however, we can write a class ourselves so that the objects in it have a "constant" effect). This restriction also applies to arrays, which also belong to objects.

The Fnail method has two main functions: (1) to prevent the inheritance class from modifying the method, (2) to improve the efficiency, provided that the method code quantity is less. There are also private methods in the class that cannot be accessed, will be automatically added by the compiler final keyword, of course, if you can also actively add final, but without any additional significance.

Final class , not allowed to be inherited.

Conclusion: Final fields or values can only be assigned once, when defined, it is possible to define empty final, but it must be assigned before use, and once assigned, the final value cannot be changed again.

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.