Java initialization sequence, java Initialization
Java variable types are as follows:
Instance variables;
Class variable;
The initialization method is as follows:
Instance variables
-- Initialization during Declaration;
-- Non-static initialization within the block, initialization;
-- Initialization within the constructor;
Instance 1:
public class bean1 {int d;int e;int c;int b;int f;{c = 3;}int a = 1;{b = 2;}public bean1() {d = 4;f = 5;e = 6;}public int geta() {return a;}}
Javap-c bean1.class:
Compiled from "bean1.java" public class initialization. bean1 {int d; int e; int c; int B; int f; int a; public initialization. bean1 (); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object. "<init>" :() V 4: aload_0 5: iconst_3 // initialize c 6: putfield #2 // Field c: I 9: aload_0 10: iconst_1 // initialize a 11: putfield #3 // Field a: I 14: aload_0 15: iconst_2 // initialize B 16: putfield #4 // Field B: I 19: aload_0 20: iconst_4 // initialize d 21: putfield #5 // Field d: I 24: aload_0 25: iconst_5 26: putfield #6 // Field f: I 29: aload_0 30: bipush 6 32: putfield #7 // Field e: I 35: return public int geta (); Code: 0: aload_0 1: getfield #3 // Field: I 4: ireturn}
Conclusion: The initialization of instance variables is in public initialization. bean1 () is completed. Whether it is initialized directly at the time of declaration or in the code block, it is initialized in the constructor in essence, the initialization sequence of the constructor depends on the sequence in the definition direct initialization and code block initialization.
Class variable
-- Initialization during Declaration;
-- Initialize the static initialization block;
Instance 2:
package initialization;public class bean2 {static int d;static int e = 5;static int c;int b;int f;int a = 1;static {c = 3;System.out.println("hello,static");}{b = 2;System.out.println("hello");}static {d = 4;}public bean2() {f = 6;}public int geta() {return a;}}
When creating a bean2 instance, the output is as follows:
hello,statichello
Static code blocks are actively executed and need to be initialized when the project is started;
Javap-c bean2.class:
Compiled from "bean2.java" public class initialization. bean2 {static int d; static int e; static int c; int B; int f; int a; public initialization. bean2 (); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object. "<init>" :() V 4: aload_0 5: iconst_1 // initialize a 6: putfield #2 // Field a: I 9: aload_0 10: iconst_2 // initialize B 11: putfield #3 // Field B: I 14: aload_0 15: bipush 6 17: putfield #4 // Field f: I 20: return public int geta (); Code: 0: aload_0 1: getfield #2 // Field a: I 4: ireturn static {}; Code: 0: iconst_5 // initialize e 1: putstatic #5 // Field e: I 4: iconst_3 // initialize c 5: putstatic #6 // Field c: I 8: iconst_4 // initialize d 9: putstatic #7 // Field d: I 12: return}
Conclusion: The initialization of instance variables is in public initialization. bean2 () is completed. Whether it is initialized directly at the time of declaration or in the code block, it is initialized in the constructor in essence, the initialization sequence of the constructor depends on the sequence in the definition direct initialization and code block initialization;
The class variable is completed in static {} and the initial order is mainly in the code order. The class variable is executed before the instance variable;
For java final, you can view: http://www.cnblogs.com/dolphin0520/p/3736238.html