write this article to review the Java static block, Main method, building blocks, construction methods of the order of execution, directly run the following code can be at a glance, no more said.
/** * Run the Parent class Main method The order of execution of each block scope: * Static block (and only once)-> Main method-> construction method of building block->/public class Parent {privat
e int age = 0;
Private String name = NULL;
private static String company = NULL;
static {System.out.println ("Parent static Block runing");
Company = "Parent-ahik";
{System.out.println ("Parent constructor block runing");
THIS.name = "parent";
Public parent () {System.out.println ("Parent construct method parent () runing");
This.age = 28;
Public parent (int age) {System.out.println ("Parent construct method parent (int age) runing");
This.age = age;
@Override public String toString () {return ' parent{' + ' age= ' + Age +
", Name= '" + name + ' \ ' + ", company= '" + Company + "'" + '} '; public static void Main (String ... args) {System.out.println ("Parent main")Runing ");
Parent P1 = new parent ();
System.out.println (p1);
Parent P2 = new parent (33);
System.out.println (p2);
{int i = 100;
System.out.println (i);
int i = 99;
System.out.println (i); }}///////////////////////////////////////////////////////////////** * Run Subclass Main Method The order of execution of individual block scopes: * Parent class static block (and only execute once -> * Subclass static block and execute only once)-> * Subclass Main Method-> * Parent class building Block-> * Parent class construction Method-> * Subclass Construction Block-> * Subclass Construction Method/Public
Class Child extends Parent {private int age = 0;
Private String name = NULL;
private static String company = NULL;
static {System.out.println ("Child static Block runing");
Company = "Child-ahik";
{this.name = "child";
SYSTEM.OUT.PRINTLN ("Child construct block start runing");
Public Child () {super (66);
This.age = 99; SYSTEM.OUT.PRINTLN ("Child construct Method child () runing");
(int age) {Super (77);
This.age = age;
SYSTEM.OUT.PRINTLN ("Child construct (int age) runing");
public static void Main (String ... args) {System.out.println ("Child main runing");
Child C1 = new Child ();
SYSTEM.OUT.PRINTLN (C1);
Child C2 = new Child (100);
System.out.println (C2);
}
}