// order of execution :(priority from high to low.) Static code block > Mian Method > Construct code Block > Constructor.
The static code block is executed only once. The construction block is executed each time an object is created.
1 Common code block
1 // Plain code blocks: {} that appear in a method or statement are plain code blocks. 2 Public class CodeBlock01{3 public static void main(String[] args){4 5 {6 public static void main(String[] args){4 5 {6 int x=3; 7 system.out. println("1, x="+x); 8 } 9 10 int x=1; 11 system.out. println(" x="+x); 12 13 { 14 int y=7; System.out.println("2, y="+y); 16} 17} 18} 19 20 /* 21 Run result: 22 1, ordinary code block variable x=3 23 main method variable x=1 24 2, ordinary code block variable y=7 25 */Copy the code
2 Construct code blocks
1 // Constructor block: code blocks defined directly in a class without the static keyword are called {} constructor blocks. The constructor code block is called when the object is created, every time the object is created, and the constructor code block takes precedence over the class constructor. 2 3 public class CodeBlock02{4 {5 system.out.println (" first code block "); 6} 7 8 public CodeBlock02(){9 system.out.println (" constructor "); 10} 11 11 {13 system.out.println (" second constructor "); 14 } 15 public static void main(String[] args){ 16 new CodeBlock02(); 17 new CodeBlock02(); 18 new CodeBlock02(); 19 20} 21} 22 23 /* 24 * 25 Execution result: 26 First code block 27 Second construction block 28 Constructor 29 First code block 30 Second construction block 31 Constructor 32 First code block 33 Second construction block 34 Constructor 35 */Copy the code
Static code block
1 // Static code block: a code block declared in Java using the static keyword. Static blocks are used to initialize a class for its properties. Each static code block is executed only once. Because the JVM executes a static code block when it loads a class, the static code block executes before the main method. 2 // If the class contains more than one static code block, the code defined first is executed, and the code defined later is executed. 3 // Note: 1 Static code blocks cannot exist in any method body. Static code blocks cannot access static instance variables and instance methods directly; they need to be accessed through class instance objects. 4 5 6 class Code{7 {8 system.out.println ("Code building block "); 9} 10 11 static{12 system.out.println (" static Code block "); 13} 14 15 public Code(){16 system.out.println (" constructor of Code "); 17} 18} 19 21 public class CodeBlock03{22 {23 system.out.println ("CodeBlock03 constructor "); 24} 26 static{27 system.out.println ("CodeBlock03 static code block "); 28} 29 30 public CodeBlock03(){31 system.out.println ("CodeBlock03 constructor "); 32} 34 public static void main(String[] args){35 system.out.println ("CodeBlock03 "); 36 new Code(); 37 new Code(); 38 new CodeBlock03(); 39 new CodeBlock03(); 40} 41} 42 /* 43 CodeBlock03 static Code block 44 CodeBlock03 main method 45 Code static Code block 46 Code constructor 47 Code constructor 48 Code constructor 49 Code constructor 50 CodeBlock03 construction block 51 CodeBlock03 construction block 52 CodeBlock03 construction block 53 CodeBlock03 construction method 54 */Copy the code