Java code block and constructor execution sequence

Source: Internet
Author: User

The code block here refers to the code block enclosed by {}, which is independent of the method body, including static code blocks and non-static code blocks. For example:

 

Class {

Static int x = 0; // static attribute

Int y = 1; // non-static attribute

Static {

// Static code block

}

 

{

// Non-static code block

}

 

Public (){

// Constructor

}

 

Public static void main (string [] ARGs ){

A class1; // load the class

A = new A (); // instantiate and create an object

}

}

 

The execution sequence can be summarized as follows:

 

1. Static attributes and static statement blocks are all completed during class loading, and non-static objects are executed during object creation.

2. the initialization sequence of a single class is as follows:
Allocate and assign values to static attributes-> execute static code blocks-> allocate memory for non-static attributes and assign values-> execute non-static code blocks-> constructor.

3. the initialization sequence of the inherited classes is as follows:
Parent static-> parent static block-> child static block-> parent non static block-> parent structure-> child non static-> sub-non-static blocks-> sub-structures.

 

View articles for details

Http://topic.csdn.net/u/20100302/17/b796145c-2495-4c42-8ea1-af5c76e85e4b.html

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.