Java prerequisites: initialization Blocks

Source: Internet
Author: User

I. Concepts

  • Function: similar to the constructor, the initialization block can also initialize Java objects.

  • Nature: it is the 4th types of members (Field, method, and constructor) that can appear in the Java class. It has no name or identifier, so it cannot call the initialization block through the class or object class. The initialization block is only implicitly executed when a Java object is created and executed before the constructor is executed.

    Ii. Use

    • Syntax format:

      [Modifier] {

      // Initialize the executable code of the block

      }

      • The modifier can only be static. A static initialization block is called a static initialization block.

      • The code in the initialization block can contain any executable statements, including defining local variables, calling methods of other objects, and using branch and loop statements.

        3. initialize the block and constructor

        • To some extent, the initialization block is a supplement to the constructor, and the initialization block is always executed before the constructor.

          • Difference from the constructor: the initialization block is a fixed piece of code that cannot accept any parameters. Therefore, the initialization block performs the same Initialization on all objects in the same class.

            • Application Scenario: if a piece of initialization code is identical to all objects without any parameters, you can extract the initialization code to the initialization block.

            • Advantages: by extracting the same code from multiple constructors into the initialization block, we can better improve the reuse of the initialization code.

              Iv. Static initialization Block

              • Static initialization blocks are class-related. The system will execute static initialization blocks in the class initialization phase, rather than when creating objects. Therefore, static initialization blocks are always executed first than normal initialization blocks;

              • It usually initializes the class Field and Cannot initialize the instance Field;

              • A static member of the class and cannot access non-static members;

                5. execution sequence

                1. There can be multiple constructors in a class, and the initialization blocks of the same type are ordered: the initialization blocks defined earlier are executed first, and the initialization blocks defined later are executed later;

                2. Common initialization blocks and the default value specified by the declared instance Field can be considered as the initialization code of objects. Their execution sequence is the same as that in the source program;

                3. When creating a Java object, the system will not only execute the common initialization block and constructor of this class, but also continue to roll back to Java. lang. object Class, first execute Java. lang. initialize the block of the Object class and start to execute Java. lang. object constructor. Execute the initialization block of its parent class in sequence and start executing the constructor of its parent class...... Finally, the initialization block and constructor of the class are executed, and the returned object is returned.

                4. When the system executes the static initialization block in the class initialization phase, it not only executes the static initialization block of the class, but also traces back to Java. lang. object Class (if it also contains static initialization blocks), first execute Java. lang. object Class static initialization block (if any), and then execute the static initialization of the parent class fast...... Finally, the static initialization block of this 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.