Java Inherits class load order

Source: Internet
Author: User

Package Xu.jin;class insect{private int i=print ("Hello i"), insect () {System.out.println ("insect" +i);} {System.out.println ("1"); System.out.println ("2");} Static{system.out.println ("3"); System.out.println ("4");} static int print (String str) {System.out.println (str); return 2;} static int X1=print ("static Insect");} Class Hello extends insect{  private int J=print ("Hello J");  static int X2=print ("static Hello");  Hello () {  System.out.println ("Hello" +j);  }  {System.out.println ("11"); System.out.println ("22");} Static{system.out.println ("33"); System.out.println ("44");} public static void Main (string[] args) {//TODO auto-generated method Stubhello scat=new Hello ();//insect ins=new insect () ;     System.out.println ("Hello World");}}

Output

34static insectstatic hello3344hello i12insect2 Hello J1122hello2hello World

      • The class is loaded first, and then initialized. If you think about it, you must first load the base class, and then the subclass, the initialization is also, the subclass is added on the basis of the base class, must first initialize the base class again subclass.
      • Initializes the static variable, which needs to be loaded before executing the main () method. Then the main method, if new, initializes the basic member variables of the object class (non-Methods), including building blocks of code, which are executed sequentially in writing, and then calling the constructor.
      • With regard to the initialization mechanism of inheritance, first execute the class containing the main method and observe that the Hello class contains the base class insect, that is, load the static variable of the insect class first, and then load the static variable of the Hello class. After the static variable is loaded, the main () method is called, and new Hello initializes the base variable and constructs the block of code, and then calls the constructor of the base class. Then initialize the basic variables of the subclass Hello and construct the code block, then execute the subclass constructor.


Java Inherits class load 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.