Java program initialization process detailed-__java

Source: Internet
Author: User

Http://www.sunxin.org/forum/thread/23317.html

The overall order of Core Java in the Java initialization process is not spoken, but the order in which the constructor is made is probably the author's view that there are many paths that are confusing to list. I think it is better to find out the process. So now, combine my learning experience to write a specific process:

The process is as follows:

1. See if there are any static elements in the declaration of a class (static element, let me call it that), such as:


static int x = 1, {//block float sss = 333.3;  String str = "Hello";   Or, for example, static {//(static block), int x = 2;   Double y = 33.3; }

If a static element executes the statement first, but note that the static element executes only once, and when the object of the class is created the second time, the statement of the static element is not executed.

2. See if this class is a startup run class, and execute the statement corresponding to the main () method if the class is started.

3. If the running class is not started, then the variable assignment of the non-static element and the code block are continued in the order of typesetting of the code.

4. The final implementation of the construction method, if you have the This keyword in the called constructor (note that if you consider calling other constructs, you should write this to the front, or you will generate an error), call the corresponding constructor, and then execute your own remaining statements after the call is complete.

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.