Initialization Order of Java

Source: Internet
Author: User

Order of initialization:

1. Initialize the object memory space to binary 0 (all data members are set to the default value)

2. If the class has a base class, initialize its base class (call the default base class constructor, or you can specify a constructor in the subclass constructor that calls the base class)

3. Static members and static domains (who are first initialized, and only a single space in memory, different objects of the same class can be shared)

4.main method

5. Non-static member variable, reference, instance initialization initialized

6. Constructors

1,2,3,4 is the class loading process, 5,6 is the new object procedure, and only new executes.

classa {a () {System.out.println ("Base class constructor"); System.out.println ("Proof data member is set as default");    Draw ();    } A (String s) {System.out.println (s); }    voidDraw () {System.out.println ("A.draw ()"); }} Public classTestextendsA {inti = 5; Test () {System.out.println (Sub-class constructor); System.out.println ("proves that the data member was initialized before the constructor call");    Draw (); } String b= "non-static member"; A A=NewA (b + "and non-static reference"); {A B=NewA ("Instance initialization"); } String C= "non-static member and instance initialization peer, who initialized before first"; A D=NewA (c); voidDraw () {System.out.println ("I =" +i); }    StaticA start =NewA ("---class loading---"); StaticString e = "static Member"; StaticA f =NewA (E + "and Static Reference")); Static{A g=NewA ("Static domain"); }    StaticString h = "static member vs. static domain, who initialized first"; StaticA j =NewA (h);  Public Static voidMain (string[] args) {System.out.println ("Main Execution"); System.out.println ("---Create the test object---"); Test k=NewTest (); }}

Output Result:

---class loads---static members and static references to static domain static members with static domain peers, who first initialize who main executes ---Create the test object---= 0= 5




Initialization Order of Java

Related Article

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.