Java Classes and objects

Source: Internet
Author: User

Please enter and run the following code to get what results ?

Procedures and Results:

Two, why the following code can not be compiled? Where did it go wrong?

Procedures and Results:

Conclusion: If a class provides a custom construction method, the default construction method is no longer provided by the system.

Third, according to the output of the Code, self-summary Java field initialization law.

Program:

Results:

rule: When Java initializes the same variable many times when it is initialized, Java assigns the last initialization result to the variable.

When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block.

Program:

Results:

Summarize:

Creating a Subclass object causes the parent class to initialize the blocks in the order that they are executed:

Initializes the static initialization block of the parent class (the highest level), the parameterless constructor of the parent class, the static initialization block of the subclass, and the parameterless constructor of the subclass;

Then is the normal initialization block of the parent class, the normal initialization block of the subclass, the parameter constructor of the parent class, and the parameter constructor of the subclass.

The order in which static initialization blocks are executed is:

Initializes the static initialization block of the parent class (the highest level), the parameterless constructor of the parent class, the static initialization block of the subclass, and the parameterless constructor of the subclass;

Then is the normal initialization block of the parent class, the normal initialization block of the subclass, the parameter constructor of the parent class, and the parameter constructor of the subclass.

Static initialization blocks are executed only once. When you create an object of a subtype, it also causes the execution of the static initialization block of the parent type.

V: accessing instance members of a class in a static method

Program:

Six: " Magic code ",Integer " weird " features

Program:

Results:

Cause: It calls the valueOf method in the Integer as follows:

The above code shows that when the value is-128~127, it returns a pointer to theIntegercache.cachea reference to an object that already exists in the, otherwise creates a newIntegerobject. BecauseI1and theI2the value is -, so it will be directly fromCachetakes an already existing object, soI1and theI2point to the same object, andi3and theI4it points to different objects, respectively.

Java Classes and objects

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.