Crazy Java Handouts (11)----initialization block

Source: Internet
Author: User

    • Normal initialization block

There can be more than one initialization block in a class, and there is a sequence between initialization blocks of the same type: the first execution defined earlier, and the subsequent execution after the definition.

The modifier for the initialization block can only be static, and the initialization block using the static modifier is called a static initialization block.

The default values specified by the normal initialization block, the declaring instance property, are considered initialization code for the object, and the order of execution is the same as the declaration order.

When Java creates an object, the system allocates memory for all instance properties of the object (provided that the class has already been loaded), and then the program begins to initialize these instance properties, starting with the initial values that were established when the block was initialized or the property was declared, and then the initial values specified in the constructor are executed.

Similar to the constructor, when a Java object is created, not only the normal initialization blocks and constructors of the class are executed, but the system goes back to the object class, executes the initialization block of the object class, then the constructor of object, then executes the initialization block of its parent class, then the constructor of the parent class ... Finally, the initialization block and the constructor of the class are executed, returning the object of the class.

    • Static initialization blocks

Static initialization blocks are class-dependent, and the system executes static initialization blocks during the class initialization phase, rather than when the object is created. Therefore, static initialization blocks are always earlier than normal initialization blocks. And it can only initialize class properties, and cannot initialize instance properties.

When a static initialization block is executed during the class initialization phase, the static initialization blocks of this class are not only executed, but also traced back to the object class, the static initialization block of the parent class, and finally the static initialization block of the class, which can be used in the system only after the class initialization is complete.

When you first create a class object, because the class does not exist in the system, you need to load and initialize the class first, initialize the static initialization block of its top-level parent class first, then the static initialization block of the immediate parent class, and finally execute the static initialization block of the class itself. Once the class is initialized, it will persist in the virtual machine, so you do not need to call the static initialization block of the class the second time you create the object.

When you really start creating the class object, the initialization block of the topmost parent class is executed first, the constructor is executed, and then the initialization block of its parent class, the constructor 、、、 finally executes its own initialization block, constructor.

The Java system loads and initializes a class, always guaranteeing that all parent classes of the class, including the immediate parent class and the indirect parent class, are all loaded and initialized.

Crazy Java Handouts (11)----initialization 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.