Instance parsing constructor initialization in Java

1. Initialization sequence when Java creates an object, The system first allocates memory for all instance properties of the object (provided that the class has been loaded), then the program starts to initialize these instance attributes. The initialization order is: first execute the initial value formulated when initializing the block or declaring the attribute, and then execute the initial value formulated in the constructor. Within the class, the order of variable definition determines the initialization order, and the variables are scattered between method definitions They will still be initialized before any methods (including constructors) are called.

Operation results:

As can be seen from the output, W3 this reference is initialized twice: once before calling the constructor and once during the call (the first referenced object will be discarded and garbage collected).

2. Initialization of static data no matter how many objects are created, static data only occupies one storage area. The static keyword cannot be applied to local variables, so it can only act on domains.

Operation results:

To some extent, initialization is a piece of fixed execution code that cannot accept any parameters. Therefore, the initialization block initializes all objects of the same class exactly the same. For this reason, it is not difficult to find the basic usage of the initialization block. If there is an initialization processing code that is exactly the same for all objects and does not need to accept any parameters, this initialization processing code can be extracted into the initialization block.

The above is all about the constructor initialization in instance parsing Java in this paper. I hope it will be helpful to you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>