Transfer from http://blog.csdn.net/monghuan/article/details/7404670
A class can use static blocks of code that are not contained in any method body, and when a class is loaded, a static block of code is executed and executed only once, and static chunks are commonly used to perform initialization of class properties.
I. Class loading, the initialization order of variables:
1. First, assign a memory space to the static member variable and initialize it by default
(int is 0, float type is 0.0, Boolean is false, character type is ' \u0000 ', reference type is null)
2. Second, perform initialization of static member variables
--Initialization of static members, including two types: direct initialization and static block of code when declaring
--Order of execution: the order in which they appear in the code (the Order of declarations)
Note: Actions 1 and 2 are performed only once when the class is loaded!!!!!!!
If an object is created, the instance of the class is allocated memory space in the heap,
3. First, initialize the instance variable with the default
(int is 0, float type is 0.0, Boolean is false, character type is ' \u0000 ', reference type is null)
4. Second, perform initialization of instance variables
--Initialization of instance variables, using the first 2 types of initialization: direct initialization and code block at declaration
--Order of execution: the order in which they appear in the code (the Order of declarations)
5. Finally, execute the constructor (the 3rd initialization of the instance variable)
Two. When inheriting, the variables are initialized in order:
1. When the class is first used, the JVM loads the class, and if the class has a parent class, it loads the parent class first, which is a recursive process until object.
In class loading, first static member variables are initialized by default values,
The display initialization of static code blocks and static variables is then performed in the order in which they are declared in the class.
This process is from the parent class to the subclass and executes only once!!!
2. When the static code of the parent class and the subclass is initialized, if the object of the class is created,
Before the subclass is initialized, the instance variables of its parent class are initialized by default.
It then executes the display initialization of the code block and instance variables in the order in which they are declared in the class.
Finally, the constructor of the parent class is called, which is also a recursive procedure until the object class.
(This process executes!!! each time the object is created)
Initialization process for Java classes