1. Initialization of class
For class initialization: Class initialization is generally only initialized once, and class initialization mainly initializes static member variables.
The compilation of a class determines the initialization process of the class.
The class file generated by the compiler mainly makes the following changes to the classes defined in the source file:
1) First declare member variables inside the class in the order in which static member variables are defined.
2) Initialize according to the initialization sequence of member variables in the original java class.
The corresponding conversion between a java class and the compiled class is as follows:
Source file:
Copy the code code as follows:
public class Person{
public static String name="Zhang San";
public static int age;
static{
age=20;
System.out.println("Initialization age");
}
public static String address;
static{
address="Beijing";
age=34;
}
public static void main(String[] args) {
System.out.println(name);
System.out.println(age);
System.out.println(address);
}
}
When the java source code is converted into a class file, it is converted into code similar to the following:
Copy the code code as follows:
public class Person{
public static String name;
public static int age;
public static String address;
static{
name="Zhang San";
age=20;
System.out.println("Initialization age");
address="Beijing";
age=34;
}
public static void main(String[] args) {
System.out.println(name);
System.out.println(age);
System.out.println(address);
}
}
The initialization sequence is executed sequentially according to the initialization sequence of the corresponding class member variables after conversion, so all static member variables are declared first and then assigned, and the order of assignment is also based on the order in which the static member variables are initialized in the source code. , Note: Defining a member variable and initializing it directly is equivalent to initializing it in a static code block, both in the order in which they are defined in the source code.
2. Generation of objects
The initialization process for object generation is similar to the initialization process of a class, but a constructor stage will be added. The source code is as follows:
Copy the code code as follows:
public class Person{
{
name="李思";
age=56;
System.out.println("Initialization age");
address="Shanghai";
}
public String name="Zhang San";
public int age=29;
public String address="Beijing";
public Person(){
name="Zhao Liu";
age=23;
address="Shanghai";
}
}
After the compiler converts it into a class file, it will be converted into code similar to the following:
Copy the code code as follows:
public class Person{
public String name;
public int age;
public String address;
public Person(){
name="李思";
age=56;
System.out.println("Initialization age");
address="Shanghai";
name="Zhang San";
age=29;
address="Beijing";
name="Zhao Liu";
age=23;
address="Shanghai";
}
}
It can be seen that the initialization of member variables in the class and the code in the code block have all been moved to the constructor, and the member variables are initialized sequentially according to the initialization order of the java source file, while the code in the original constructor It is moved to the last execution of the constructor. I have never had a deep understanding of the class initialization process before. I just couldn't figure out how to initialize it. I could only remember the initialization sequence according to the book, but I forgot it after a while, so this time I finally figured it out. It is better to explain the initialization mechanism according to a model. There is no need to memorize it anymore. Only if you understand it can you not forget it.