Static blocks and instance blocks for Java

Source: Internet
Author: User

1. When a class is used for the first time, it needs to be loaded by the ClassLoader, and the loading process involves the following two points:

(1) When loading a class, if its parent class has not yet been loaded, its parent class must first be loaded;

(2) When a class is loaded into memory, its static data fields and static blocks are executed in the order in which they appear in the code.

2. When invoking the constructor of a class, the calling procedure involves the following three points:

(1) Call the constructor of the parent class;

(2) Initializing the Instance data field and implementing the instance block in the order of occurrence in the code;

(3) executes its constructor body.

public class Initializationdemo {public     static void Main (string[] args) {                 new Initializationdemo ();    }         Public Initializationdemo () {                 new B ();    }         {        System.out.println ("2. The instance block of the initialization. ");    }         static {        System.out.println ("1. static block of initialization. ");    } }  Class A {         a () {        System.out.println ("6. The constructor body of a. ");    }         {        System.out.println ("5. The instance block of a. ");    }         static {        System.out.println ("3. Static block of A. ");    }}  Class B extends A {         B () {        System.out.println ("8. The constructor body of B. ");    }         {        System.out.println ("7. The instance block of B. ");    }         static {        System.out.println ("4. Static block of B. ");    }}

  

The above example outputs the corresponding text in each part of the class so that the order of execution is seen from the output.

The corresponding output is as follows:

1. Static block of initialization.

2. The instance block of the initialization.

3. a static block.

4. Static block of B.

5. The instance block of a.

6. A's constructor body.

7. The instance block of B.

8. The constructor body of B.

The output shows the sequence of execution of the program as follows:

1. First, to use the initialization class, the initialization class is loaded, so its static block is executed and the output is 1;

2. The program calls initialization's constructor, so the instance block of the initialization class is executed, and the output is 2;

3. Then execute the initialization constructor body, which is new B (), and load it because class B is to be used;

4. Because the parent class of Class B is a, before loading B, load a, so this time to execute a loading process, that is, execute a static block, then output 3;

5.A after loading, this time to B load, then execute b loading process, that is, execute B static block, then output 4;

6. After the load is completed, the code executes new B (), the constructor of B, so the constructor of the parent class A is called first, then executes the instance block and the constructor body of a, and outputs 5 and 6 in turn;

7. Finally, finally to the "real" implementation of B's constructor (because it has been said to execute new B (), but has been called other), then executed the instance block of B and the constructor body, output 7 and 8.

Static blocks and instance blocks for Java (GO)

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.