The Java variable types are as follows:
instance variables;
class variables;
The initialization pass is as follows:
Instance variable
--when declaring, initialize;
--initialization in a non-static initialization block;
--within the constructor, initialize;
Example 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 View the disassembly code as follows:
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 A:I 4:ireturn}
Conclusion : The initialization of the instance variables is done in public initialization.bean1 (), whether it is initialized directly in the declaration, or initialized in the code block, the essence is initialized in the constructor function. The order in which the constructors are initialized depends on the order in which the direct initialization and code block initialization are defined.
class variables
--when declaring, initialize;
--initialization in static initialization blocks;
Example 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 you create a new bean2 instance, the output is as follows:
Hello,statichello
Static code blocks are executed proactively and need to be initialized when the project is started;
Javap-c Bean2.class View the disassembly code as follows:
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//initialization 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 the instance variables is done in public initialization.bean2 (), whether it is initialized directly in the declaration, or initialized in the code block, the essence is initialized in the constructor function. The order of initialization in the constructor depends on the order in which the direct initialization and code block initialization are defined;
Class variables are completed in static {}, and the initial order is dominated by the order of the code, and class variables are executed before the instance variables;
About Java Final can view: http://www.cnblogs.com/dolphin0520/p/3736238.html
Java Initialization Order