Java initialization (initialization) actually consists of two parts:
1. Initialization of classes (initialization Class & interface)
2. Creation of objects (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, this is still a separate place.
Order:
It is definitely the first step to load a class, so the initialization of the class is in front. The approximate order of initialization is:
Class initialization, subclass constructor, parent class constructor, instantiation member variable, continue execution of the subclass constructor statement
The following examples, specific explanations.
1. The initialization of classes (initialization classes and interfaces) is actually very simple, specifically:
(a) The initialization class (initialization of Class), which is the initialization of the static field and the execution of the static initialization block.
For example:
class Super {staticstatic// initialize static field, where "=" Initialization static field "" Also called static field initializer// static initialization block, also called static initializer, or static initialization block Static static initializer ");}}
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 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.
To help understand the last two o'clock, try the following example:
Public classInitialization { Public Static voidMain (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 classSystem.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 constantSystem.out.println ("-------------------------"); System.out.println (Sub.z= 2004);//Would cause initialization of Sub class//will cause the sub to initialize}}classsuper{Static intx = 2006;}classSubextendsSuper {Static Final inty = 2005;Static intZ;Static{System.out.println ("Initialization Sub");}}
2. Object creation (creation of new class instances), a little cumbersome, the steps are as follows
(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
Member variables are actually initialized 2 times, the first time is to give the default value, the second time you want to set the value, some Java video in the face of this part of the explanation some problems.
Last look at an example:
Public classInitializationorder { Public Static voidMain (string[] args) {Subclass SB=Newsubclass ();}}classsuper{Static{System.out.println (1);} Super (inti) {System.out.println (i);}}classSubclassextendsSuperImplementsinterface{Static{System.out.println (2);} Super su=NewSuper (4); Subclass () {Super(3);NewSuper (5);}}Interfaceinterface{StaticSuper su =NewSuper (0);}
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";
Execution order of Java initialization