Java inheritance initialization sequence

Source: Internet
Author: User
package Study.Initialization;class Base{private String str1=init("str1");private String str2=init("str2");public static String str3=init("static str3");public Base(String str1,String str2){System.out.println("Run Construction Base");this.str1=str1;this.str2=str2;}protected static String init(String str){System.out.println(str);return str+" init";}}class Derived extends Base{    private String str4=init("str4");    public static String str5=init("static str5");    public Derived(String str1,String str2,String str4){    super(str1,str2);    System.out.println("Run Construction Derived");    this.str4=str4;    }}public class extendInitialization {public static void main(String[] args) {System.out.println("run extendInitialization.main()");Derived d=new Derived("construction str1","construction str2","construction str4"); System.out.println("----------------------------------------");Derived d2=new Derived("construction str11","construction str22","construction str44"); }}

Running result:

Run extendinitialization. Main ()
Static str3
Static str5
Str1
Str2
Run Construction Base
Str4
Run construction derived
----------------------------------------
Str1
Str2
Run Construction Base
Str4
Run construction derived
Summary:
The initialization sequence of derived:
1. initialize static members of the base class in the defined sequence
2. initialize static members of the extension class in the defined sequence
(The above two items are run when the extension class object is defined for the first time)
3. Perform the specified initialization of the base class in the defined sequence
4. Execute the constructors of the base class
5. Execute the specified initialization of the extension class in the defined sequence.
6. constructor of the row extension class

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.