Java class Initialization process

Source: Internet
Author: User

     1  class X {     2      y b = new y ();     3      x ()  {     4            system.out.print ("X");     5     }      6  }     7        8   class y {     9     y ()  {     10           system.out.print ("Y");     11     }    12  }     13       14  public class z extends  x {   &nbsP;15     y y = new y ();    16      z ()  {    17            system.out.print ("Z");    18     }     19       20     public static void  main (String[] args)  {    21            new z ();    22     }     23  }

Class before calling the constructor, first executes the statement that initializes the class member, and if other classes are inherited, the constructor of the parent class is called before the class member initialization statements of this class are executed, so the order of execution is (identified by the line number): 21,16,3,2,9,10 (Y), 3,4, (X), 16,15,9,10 (Y), 16,17 (Z), and then proceeds to the end. () The output of the character in the line, so the result is yxyz.

Attached: The JDB command is a Linux gdb-like debug command that comes with the JDK and can be run on the command line, as detailed in the documentation.



Java class Initialization process

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.