1. Initialization reason:Avoid null pointing exceptions.
2. class loading and initialization sequence:
2.1 class loading time:
The compilation code of each class exists in its own independent file. The file is usually loaded when the first object of the class is created, or the static domain or static method of the category class.
2.2 initialization sequence: (first parent class and then subclass. initialize the fields in the class before creating the object)
First, the Class Loader takes the initiative to load the main class (the initialization sequence of the main class is the same as that of the common class below), find the main method, in the main method according to the object to be created in the first line, load the bytecode file of this class. If the compiler notices that she has a base class (this is known by the extends keyword), it will continue to load (whether or not it is necessary to create a base class object, this will happen .) In the base class, the first static initialization and the static code block are child classes in sequence. Now the class loading, static initialization, and static code execution are completed, and the object can be created. First, all the fields in the object are set to the default value. Then, the common fields are initialized before the parent class constructor is called, and the initialization is executed until the initialization is completed.
3. Code Verification:
Class person {
Private Static string city = "Shanxi ";
Private string name = new string ("Wh ");
Private string age;
Static {
System. Out. println (City + "perosn static code block ");
}
{
System. Out. println (name + "common block" + age );
}
Public Person (string name, string age ){
System. Out. println (City + "---------->" + this. Name + "----------->" + this. Age );
This. Name = Name;
This. Age = age;
System. Out. println (City + "---------->" + this. Name + "----------->" + this. Age );
}
}
Class student extends person {
Private Static string school = "XD ";
Private string sname = new string ("Sn ");
Private string Sage;
Static {
System. Out. println (School + "Student static code block ");
}
{
System. Out. println (sname + "Student common block" + SAGE );
}
Public student (string name, string age ){
Super (name, age );
System. Out. println (student. School + "------------>" + this. sname + "------->" + this. Sage );
This. sname = Name;
This. Sage = age;
System. Out. println (student. School + "------------>" + this. sname + "------->" + this. Sage );
}
}
Public class initsort {
Public static void main (string [] ARGs ){
Student S1 = new student ("cjt", "23 ");
Student S2 = new student ("CJR", "21 ");
}
}
Output:
Shanxiperosn static code block
XD student static code block
Wh normal block null
Shanxi ----------> wh -----------> null
Shanxi ----------> cjt -----------> 23
SN student common block null
XD ------------> Sn -------> null
XD ------------> cjt -------> 23
Wh normal block null
Shanxi ----------> wh -----------> null
Shanxi ----------> CJR -----------> 21
SN student common block null
XD ------------> Sn -------> null
XD ------------> CJR -------> 21