Java Foundation---Initialization block __java basic Learning

Source: Internet
Author: User
First, the concept
Role: Very similar to the constructor function is the initialization block, it can also initialize the Java object operation.
Property: Is the 4th member (Field, method, and constructor) that can appear in a Java class, it has no name and no identity, so it is not possible to invoke the initialization block through the class, object class. The initialization block is executed implicitly only when the Java object is created, and before the constructor is executed.

Second, the use
Syntax format:
[Modifier] {
Executable code for initialization block
}

Modifiers can only be static, initialization blocks with static adornments are called static initialization blocks
The code in the initialization block can contain any executable statements, including defining local variables, calling methods of other objects, and using branches, looping statements, and so on.

Iii. initialization blocks and constructors
In a way, the initialization block is a complement to the constructor, and the initialization block is always executed before the constructor.
Unlike a constructor: the initialization block is a piece of code that is executed in a fixed format, and it cannot accept any arguments. The initialization block therefore initializes the same initialization process for all objects of the same class.

Use scenario: If you have a section of initialization code that is identical to all objects and you do not accept any arguments, you can extract the initialization code into the initialization block.
Use benefits: The reuse of initialization code can be better improved by extracting the same code from multiple constructors into the initialization block.

Four, static initialization block
Static initialization blocks are class-related, and the system performs static initialization blocks in the class initialization phase, rather than when the object is created, so the static initialization block is always executed before the normal initialization block;
It usually performs initialization on the class field and cannot initialize the instance field;
A static member of a class that cannot access non-static members;

V. Order of execution
There can be multiple constructors in a class, and there is an order between the initialization blocks of the same type: the previously defined initialization block executes first, followed by the initialization block defined later;
The normal initialization block, the default value specified by the declaring instance field, can be considered the initialization code for the object, and they are executed in the same order as in the source program;
When a Java object is created, not only does the normal initialization block and constructor of the class execute, And the system goes back to the Java.lang.Object class, executes the initialization block of the Java.lang.Object class, starts executing the Java.lang.Object constructor, executes the initialization block of its parent class, and starts executing its parent class's constructor ... Finally, the initialization block and constructor for the class are executed, returning the object to be returned.
When the system executes a static initialization block during the class initialization phase, not only does the static initialization block of this class execute, It is also traced back to the Java.lang.Object class (if it also contains a static initialization block), executes the static initialization block (if any) of the Java.lang.Object class, and then executes the static initialization of the parent class quickly ... Finally, the static initialization block of the class is executed.

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.