Initialization of a class
Initialization of a class: Initialization of a class is typically initialized only once, and the initialization of a class is primarily the initialization of static member variables.
The compilation of a class determines the initialization of a class.
The class file generated by the compiler mainly makes the following changes to the classes defined in the source file:
1) Declare member variables inside the class first in the order in which they are defined in the static member variable.
2) Initialize the initialization order of member variables in the original Java class.
A Java class and the compiled class correspond to the following conversions:
Source file:
public class person{
public static String name= "Zhang San";
public static int age;
static{
age=20;
SYSTEM.OUT.PRINTLN ("Initialize 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 to a class file, it is converted to code similar to the following:
public class person{
public static String name;
public static int age;
public static String address;
static{
Name= "Zhang San";
age=20;
SYSTEM.OUT.PRINTLN ("Initialize 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 order is executed sequentially according to the initialization order of the corresponding class member variables, so all static member variables are declared first, followed by assignments, and the order of assignment is based on the order in which the source code initializes the static member variables. Note: Defining a member variable and initializing it directly is equivalent to initializing it in a static code block, based on the order in which they are defined in the source code.
Second, the generation of objects
The initialization process for an object is similar to the initialization of the class, but it increases the constructor phase, which is the following source code:
1 public class person{
2 {
3 name= "John Doe";
4 age=56;
5 System.out.println ("Initialize Age");
6 address= "Shanghai";
7}
8 public String name= "Zhang San";
9 public int age=29;
Ten public String address= "Beijing";
Public person () {
Name= "Zhao Liu";
age=23;
address= "Shanghai City";
15}
16}
After the compiler converts to a class file, it is converted to code similar to the following:
1 public class person{
2 public String name;
3 public int age;
4 public String address;
5 public person () {
6 name= "John Doe";
7 age=56;
8 System.out.println ("Initialize Age");
9 address= "Shanghai";
Ten name= "Zhang San";
One by one age=29;
address= "Beijing";
Name= "Zhao Liu";
age=23;
address= "Shanghai City";
16}
17}
As you can see, the initialization of member variables in a class and the code in the code block are all moved to the constructor, and the member variables are initialized in the order in which the Java source files are initialized, and the code in the original constructor is moved to the last execution of the constructor. I have always been on the class initialization process has not been a deep understanding, is not clear how it is initialized, can only follow the book said to remember the initialization sequence, but over a period of time have forgotten, so this time finally to figure out, or follow a model to explain the initialization mechanism is better ah, no longer back, It's not easy to forget if you understand.
Java class variables and member variable initialization procedures