Test code:
1 Public classTest022 {3 PublicTest02 ()4 {5System.out.println ("Test02");6 }7 Public Static voidMain (string[] args)8 {9Test03 test03=NewTest03 ();//1Ten //test03.f (); //2 One //System.out.println (test03.i);//3 A } - } - the classTest03 - { - Static inti = 3; - StaticTest02 test02 =NewTest02 (); +Test01 test01 =NewTest01 (); - + Static A { atSystem.out.println ("Static"); - } - { -System.out.println ("Nostatic"); - } - Static voidf () in { -System.out.println ("F ()"); to } + - PublicTest03 () the { *System.out.println ("Test03"); $ }Panax Notoginseng}
The result of the execution of 1 is as follows: test02statictest01nostatictest03 execution 2 results as follows: TEST02STATICF () executes 3 results as follows: TEST02STATIC3 The conclusions are as follows: 1. When you create an object, you initialize the class where the object resides, in the order of static objects > static code blocks > Generic Objects > Common code blocks > Construction Method 2. When you simply invoke a static or static method of a class, only the static behavior is initialized at this point: Static object > Static code block > static variable (static method)
Initialization order of classes in Java