Differences and code examples of ordinary code blocks, constructed code blocks, static code blocks in Java
//Execution order: (Priority from high to low.) Static code block > mian method > construct code block > construct method.
The static code block is executed only once. The constructed code block is executed every time the object is created.
1 Normal code block
//Ordinary code block: {} that appears in a method or statement is called a normal code block. The execution order of ordinary code blocks and general statements is determined by the order in which they appear in the code - "first appear first" public class CodeBlock01{ public static void main(String[] args){ { int x=3; System.out.println("1, variable x="+x); } int x=1; System.out.println("Variable x="+x); { int y=7; System.out.println("2, variable y="+y); } } } /* Running result: 1, variable x=3 in normal code block variable x=1 2, variable y=7 in normal code block variable y=7 */2 Constructing code blocks
//Constructor block: A code block that is directly defined in the class and does not have a static keyword is called a {} construction code block. The constructor code block is called when creating an object, and each time the object is created, it is called, and the execution order of the constructor code block takes precedence over the class constructor. public class CodeBlock02{ { System.out.println("first code block"); } public CodeBlock02(){ System.out.println("construction method"); } { System.out.println("second constructor block"); } public static void main(String[] args){ new CodeBlock02(); new CodeBlock02(); new CodeBlock02(); }} /**Execution result: First code block second constructor block construction method First code block second constructor block construction method First code block second constructor block construction method */3 Static code blocks
// Static code block: code block declared using static keyword in java. Static blocks are used to initialize classes and initialize the attributes of the class. Each static code block will be executed only once. Since the JVM executes static code blocks when loading the class, the static code blocks execute before the main method. //If the class contains multiple static code blocks, it will be followed by "defined code first, then defined code later". //Note: 1 Static code blocks cannot exist in any method body. 2 Static code blocks cannot directly access static instance variables and instance methods, and need to be accessed through the instance object of the class. class Code{ { System.out.println("Constructor of Code"); } static{ System.out.println("Constructor of Code"); } public Code(){ System.out.println("Constructor of Code"); } } public class CodeBlock03{ { System.out.println("Constructor of CodeBlock03"); } static{ System.out.println("Constructor of CodeBlock03"); } public CodeBlock03(){ System.out.println("Constructor method of CodeBlock03"); } public static void main(String[] args){ System.out.println("Main method of CodeBlock03"); new Code(); new Code(); new CodeBlock03(); new CodeBlock03(); } }/*CodeBlock03 Static code block CodeBlock03 Main method Code static code block Code constructor Code constructor Code constructor Code constructor Code constructor Code constructor Code constructor Code constructor Code constructor Code constructor Code block Code block CodeBlock03 Construction method CodeBlock03 Construction method CodeBlock03 Construction method CodeBlock03 Construction method */Thank you for reading, I hope it can help you. Thank you for your support for this site!