A code block is constructed in the child parent class in the code static block. Code block constructor member variable instance variable execution order

Source: Internet
Author: User

It's not easy to get it straight when it starts to touch. to create a subclass or polymorphic condition:/* When creating subclasses: 1. Parent class static code block 2. Subclass static code block 3. The parent class constructs a block/instance variable (a member of a variable created by new)//WHO executes before executing the constructor 4. Parent class Constructors//parent class constructors have methods , the parent class method is not executed if the method subclass has a method that executes the subclass. //If the subclass has a parent class that does not have an error!//parent class Private The method executes the parent class method5. Subclass constructs block/instance variable//WHO executes the constructor 6 after execution. Subclass Constructor 7. Subclass Normal Method//Call member variable, look at the value of the subclass, no longer look at the value of the parent Class 8. The child class member variable//does not call the value of the parent Class 9. Normal method Output result
Class a{      int m=1;    {System.out.println ("111");}        1-->111    A () {        speak ();//4--> directly executes the subclass method 5    }    c c = new C (); 2-->    {System.out.println ("222");}    void Speak () {        System.out.println ("a");}    }          Class c{    C () {        System.out.println ("C");//3-->c                                //7-->c    }}class B extends a{    int m = 5; 10-->    {System.out.println ("333");}    B () {        System.out.println ("B");//8-->b    }    c c = new C (); 6-->    void Speak () {        System.out.println (m);//5-->0                              //9-->                              //11-->5    }    {System.out.println ("444");}    public static void Main (string[] args) {        b b = new B ();        B.speak ();    }}

The comments are followed by the execution order and output results.

A code block is constructed in the child parent class in the code static block. Code block constructor member variable instance variable execution order

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.