Java Initialization constructor Call order

Source: Internet
Author: User

The sequence of constructor calls when class initialization:

(1) The storage space of the initialized object is zero or null value;
(2) Call the parent class constructor;
(3) The initialization expressions of class member variables and instance member variables are called sequentially;
(4) Call the constructor itself .

Example:
public class Dollar extends money{
RMB r=new RMB ()
Public Dollar () {
System.out.println ("Dollar is construct!");
}
public static void Main (string[] args) {

New Dollar ();

}
}

Class money{
Public money () {
System.out.println ("Money is construct");
}
}

Class rmb{
Public Rmb () {
System.out.println ("RMB is construct");
}
}

Output Result:
Money is construct
$ is construct
Dollar is construct!

we'll write a program ourselves to test the order of the run:


Output Result:

Parent Class The class member variable description is now run in the following order:

    1. Parent class static Block
    2. Self- class member variables
    3. Self-static block
    4. Parent class Instance member variable
    5. Parent Class Block
    6. Parent class constructor
    7. Self instance member variables
    8. Self block
    9. Self-constructor

Java Initialization constructor Call order

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.