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