From: http://blog.csdn.net/socoolfj/article/details/750425 http://www.cnblogs.com/miniwiki/archive/2011/03/25/1995615.html
Rule 1 (in case of no inheritance):
For static variables, static initialization blocks, variables, initialization blocks, constructors, they are initialized sequentially
(static variable, static initialization block) > (variable, initialization block) > constructor
Rule 2 (in case of inheritance):
The initialization of a static variable and a static initialization block for a subclass is done before the parent class's variables, initialization blocks, and constructors are initialized. static variables and static initialization blocks are initialized according to the order in which they are defined in the class . Similarly, variables and initialization blocks follow this pattern.
Parent class--Static variable
Parent class--static initialization block
Subclass--Static variables
Subclass--Static initialization block
Parent class--variable
Parent class--initialization block
Parent class--constructor
Subclass--Variables
Subclass--Initialization block
Sub-class--constructor
Summary: First static, after the father, from the top to the bottom, the first variable after the construction
Principle:
Initialization (initialization) actually consists of two parts:
(1) Initialization of the class (Initialization Class & Interface) (2) object creation (creation of new class instances).
Because class initialization is actually the last step of class loading (loading of classes), many books boil it down to the first step of "object creation." Actually just look at the angle of the problem is different. For a clearer understanding, it's still separate.
1. Initialization of classes (initialization classes and interfaces)
(a) The initialization class (initialization of Class), which is the initialization of the static field and the execution of the static initialization block.
Example: Class Super {
Static String s = "initialization static field"; Initializes the static field, where "=" Initialization static field "" is also called the static field initializer
Static initialization block, also called static initializer, or static initialization block
static {
System.out.println ("This is static initializer");
}
}
BTW, some books refer to the concept of static initializer and static field initializer, which correspond to instance initializer and instance variable initializer. The comments in the example already explain their meaning.
(b) The initialization interface (initialization of interface), which is the initialization of the field defined in the interface.
Note
--initialization classes, the class's superclass will first be initialized, but its implementation of interface will not.
--initialization classes, the class's superclass, as well as the superclass of Superlcass, will first be initialized recursively until java.lang.Object. But when Initialiazation interface, it does not need to be so, only initializes the interface itself.
--for initialization caused by a reference class variable (class field), only the class that really defines the field is initialized
--If a static field is a compile-time constant (Compile-time constant), the reference to it does not cause the initialization of the class that defines it.
Class super{
static int x = 2006
}
Class Sub extends Super {
static final int y = 2005;
static int z;
static {
SYSTEM.OUT.PRINTLN ("Initialization Sub");
}
}
public class Initialization {
public static void Main (string[] args) {
System.out.println (sub.x); Won ' t cause initialization of sub, because X is declared by S, not Sub.
does not cause initialization of the sub class, because X is defined in the Super class
System.out.println ("-------------------------");
System.out.println (SUB.Y); Won ' t cause initialization of Sub, because Y is constant.
does not cause initialization of the sub class, because Y is a constant
System.out.println ("-------------------------");
System.out.println (sub.z = 2004); Would cause initialization of Sub class
Will cause the sub to initialize
}
}
2. Creation of objects (creation of new class instances)
(a) All member variables-including the class, and the member variables in its parent class-are allocated memory space and given default values. (BTW, this is the first time the member variable is initialized)
(b) Initializes its argument variable for the constructor that is called. (If parameters are available)
(c) If the other constructors in the same class are called with this in the constructor, the constructor that is called is processed according to step (b) ~ (f).
(d) If the constructor of its parent class is called with super in the constructor, the parent class constructor that is called is processed according to step (b) ~ (f).
(e) Perform instance initializer and instance variable initializer in writing order to initialize member variables. (BTW, this is the second time the member variable is initialized)
(f) Execution of the remainder of the constructor in the order of writing.
Note
The member variable is actually initialized 2 times, the first time is given the default value, the second time you want to set the value.
Last look at an example:
Class super{
static {
SYSTEM.OUT.PRINTLN (1);
}
Super (int i) {
System.out.println (i);
}
}
Class Subclass extends Super implements interface{
static {
SYSTEM.OUT.PRINTLN (2);
}
Super Su = new super (4);
Subclass () {
Super (3);
New Super (5);
}
}
Interface interface{
Static Super Su = new super (0);
}
public class Initializationorder {
public static void Main (string[] args) {
Subclass SB = new Subclass ();
}
}
A little explanation:
First, the Java virtual machines execute the static method in the Initializationorder class, Main (), which causes initialization of the class. Begins the initialization of the Initializationorder class. The concrete steps omit to say nothing.
Then, after initialization of the Initializationorder class, the main () method is started. The statement subclass SB = new Subclass () creates a subclass object. Class subclass is initialized after the class is loaded, but because subclass has a parent class super, first initializes the Super class, initializing the block static {System.out.println (1);} is executed, print out 1;
Thirdly, after super initialization is complete, the subclass class is initialized. static {System.out.println (2);} is executed, print out 2;
Finally, the loading of the class is complete. Begins the process of entering an object that creates a subclass. The subclass class and its parent class super class are allocated memory space first, and Super Su is appended with a value of NULL;
V, when the constructor subclass () is executed, super (3) is executed. As stated in previous (d), the Super class's constructor super (int i) {...} is called and is processed by step (b) ~ (f). Therefore, the constructor for the parent class of the Super class is called recursively, and the object class is initialized according to step (b) ~ (f), but there is no input result. Final print output 3;
VI, as stated in the preceding (e), initializes the member variable su, which results in a printout of 4;
VII, as stated in the preceding (f), executes new Super (5) and prints out 5;
Finally, although subclass implements the interface interface, it does not initialize the interface when it is initialized, so the static super Su = new super (0) in the interface interface is not executed from beginning to end.
① Class Property (static variable) is defined when initialized, as in the above example, static String a = "string-a";
The initialization code in the ②static block, such as B = "String-b" In the previous example static {};
③ the initialization of an object property (non-static variable) when defined, as in the above example, String c = "stirng-c";
④ the initialization code in the constructor method (function), as in the previous example construction method, d = "string-d";
The order in which the Java program initializes