Java variable Initialization order, here has been indefinitely, interview time is often asked, but the answer has been bad, now tidy up records, later forget can come to see.
The program is divided into two parts, the first part does not consider the inheritance, the second part considers the inheritance;
(1) without regard to the case of inheritance
The code is as follows:
Public classJavatest { Publicjavatest () {System.out.println ("Performing javatest construction Method 1"); } Publicjavatest (String param) {System.out.println ("Performing javatest construction Method 2"); } Static{System.out.println ("Javatest Static code block 1"); } {System.out.println ("Javatest code block 1"); } Private Static intMax1 =getMax1 (); Private intMin1 =getMin1 (); Public intgetMin1 () {System.out.println ("Initialize member variable min1"); return0; } Public Static intgetMax1 () {System.out.println ("Initialize static member variable MAX1"); return0; } Static{System.out.println ("Javatest Static code block 2"); } {System.out.println ("Javatest code block 2"); } Private Static intMAX2 =getMax2 (); Private intMin2 =getMin2 (); Public intgetMin2 () {System.out.println ("Initialize member variable min2"); return0; } Public Static intgetMax2 () {System.out.println ("Initialize static member variable MAX2"); return0; } Public Static voidMain (string[] args) {System.out.println ("=============================="); Newjavatest (); System.out.println ("=============================="); NewJavatest ("param"); }}
To put it briefly: in this example, a total of two static code blocks, two initialization blocks, two constructors, two static member variables, two non-static member variables, and a decentralized declaration opened, this program is just to do the test, to prove the order of the various initialization methods, do not write the actual writing, Doing so can make the program very difficult to maintain.
Execution Result:
Javatest Static code block 1 initializes static member variables max1javatest static code block 2 initializes static member variables max2============================== Javatest code block 1 Initialize member variable min1javatest code block 2 Initialize member variable Min2 execute Javatest constructor method 1============================== Javatest code block 1 Initialize member variable min1javatest code block 2 Initialize member variable Min2 execute Javatest constructor Method 2
We can draw some conclusions from the above results:
Static initialization is preceded by the initialization of the instance and is executed only once. After static initialization, the space is allocated for the instance variable, the initialization is performed, and the constructor is executed last.
Specific implementation can be summarized as follows:
(1) When the class loads, allocates memory space for static member variables in the class and initializes the default values;
(2) Perform initialization of static member variables. There are two ways to initialize a static member: it is initialized directly to a static block of code at the time of declaration. The two types of initialization are performed in the order in which they appear in the class (the order in which they are declared).
(3) The above two steps will only be executed once when the class is loaded;
(4) If an object of the class is created, the memory space is allocated to the instance of the class in the heap, and the default value is initialized;
(5) Perform initialization of instance variables. There are also two ways to initialize and initialize a block directly when declaring it. Both of these methods are executed in the order in which they appear in the class;
(6) The constructor method that executes the class.
Note: Although a member variable of a class can be initialized directly for its variables at the time of Declaration, the declaration is not executed concurrently with the initialization. For static member variables, space is allocated for static member variables declared in all classes, and after each variable has a default value, initialization at the variable declaration is performed, and this initialization is performed in the order in which the static initialization blocks appear in the class. For instance variables, it is similar to a static variable.
Since the variable space allocation for a class is performed prior to initialization, there is a case after the variable is created, and before the variable is initialized. If you use this variable during this time, you may not get the result we want.
Use a small example to illustrate:
Public classTest2 {print (); } Private intMax = 9; PrivateString MaxValue; PublicString Getmax () {returnMaxValue; } Public voidprint () {MaxValue= "Max:" +Max; } Public Static voidMain (string[] args) {Test2 test2=NewTest2 (); System.out.println (Test2.getmax ()); }}
Let's look at the printing results to find out:
max:0
Under normal circumstances, you should be able to print 9, but print out 0. The problem is the call to print, because the method is called in the initialization block, and the initialization block and the instance variable are initialized at the declaration level, and are executed in the order in which they appear in the class;
The initialization block in the program appears before the max variable, so it executes before the max variable is initialized, and when the print method is called, the max variable is declared but has not yet been initialized with a default value of 0, so the result of printing is 0;
(2) Initialization sequence in case of inheritance
The initialization order of Java variables in "elaborate Java"