Java Foundation--this Constructor Forwarding & instance block, static block

Source: Internet
Author: User

Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.

This constructor forwards:

Two usages: current object
This () constructor forwards
1. This format can only be written in the constructor
2. The first line of code that can only be written in the constructor

Constructor forwarding:
Each constructor represents a selection, you can select one parameter or multiple, select one, the other parameters are the default, select all the parameters then according to customer needs to pass the desired parameters.

The constructor accepts parameters, calls the student method, and passes the arguments.
Purpose: Reduce the code duplication of the constructor.

Example: Depending on the data type, the constructor being forwarded is not the same.
Execution procedure: First executes the Main method, the new test null argument constructor, the null argument constructor calls the int type Test constructor, and the int parameter constructor calls the constructor of the string type.
So print 2 first, then return to the constructor of type int to print 3, and finally return to the empty argument constructor to print 1. Printed results: 2 3 1

Instance Block:

The initialization code, executed at the time of the new object, is executed earlier than the construction.
Can be written in a number of executions, executed in order, in front of the first execution, after the subsequent execution. An instance block can be written on a method, and it cannot be called an instance block or partition.
To create an object, instantiate the process:
1. Create object Start Initialize Member
2. Attribute members are initialized first, but no value is assigned
3. Start executing the instance block, execute sequentially, top to bottom execution, and hit the property to start assigning values.
4. Executing the constructor
5. Object construction Completed

Instance block request format: Instance blocks are executed earlier than the constructor.

Attention:
Figure 1: Initialized with the attribute, the property cannot be written behind the print. -

Figure 2: The attribute is placed in front of the error, the assignment can be attached.

An instance block can be written on a method, and it cannot be called an instance block or partition. Two int in a different area, so the same name can be used.

Static blocks:

Is the initialization method of the class, which executes when the class is loaded
Can also write many, executed in order

When will the class be loaded?
When you run a class, all classes are not loaded. (Which class the system uses, and which to load)
Declaring a variable with a class does not cause the class to load.
As long as the static members in the class are used, the class is prompted to load.
If you access its static constants it does not cause the class to load because static constants and classes are not in one memory, it is stored in constant memory.
The class can only be loaded once and will not load two times.

Static Block Request method: As with the instance block, the attribute cannot be written back.

Object-oriented Primary stage: How to convert real-life things into objects?
Object-oriented advanced stage: How to use orientation to make the program more flexible.

Java Foundation--this Constructor Forwarding & instance block, static block

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.