Execution order: (Priority from high to low.) Static code block >mian method > construct code block > Construct method.
Where static code blocks are executed only once. The construction code block executes every time the object is created.
1 Common code blocks
1//Ordinary code block: {} that appears in a method or statement is called a normal code block. Ordinary blocks of code and general execution order of statements are determined by the order in which they appear in the code-"first Occurrence first"2PublicClasscodeblock01{3PublicStaticvoidMain (string[] args) {45{6int x=3;7 System.out.println ("1, variable x= in normal code block" +x);8}910int X=1;SYSTEM.OUT.PRINTLN ("variable x= within the Main method" +x);1213{14int y=715 System.out.println ("2, variable y= in normal code block" +y "); 16 }17 }18 }19 20 /*21 run Result: 22 1, the variable within the normal code block X=323 variables within the Main method x=1 24 2, variable Y=7*/26
2 Building Code Blocks
//Building blocks: Blocks of code that are defined directly in the class and do not have the static keyword are called {} construction blocks of code. Construction blocks are called when the object is created, each time the object is created, and the order in which the code block is constructed takes precedence over the class constructor.PublicClass codeblock02{{System.out.println ("first code block" public CodeBlock02 () {System.out.println (" Construction method public static void< Span style= "color: #000000;" > main (string[] args) {new CodeBlock02 (); new CodeBlock02 (); new CodeBlock02 ();}} /** execution Result: first code block second construction block construction method first code block second construction block construction method first code block construction method of the second building block Span style= "color: #008000;" >*/
3 Static code block
//Static code block: a block of code declared in Java using the static keyword. Static blocks are used to initialize the class and initialize the properties of the class. Each static block of code is executed only once. Because the JVM executes static blocks of code when the class is loaded, the static block of code executes before the main method.//If a class contains more than one static block of code, it will follow the code defined first, then execute after the definition.//Note: 1 Static code blocks cannot exist in any method body. 2 Static code blocks do not have direct access to static instance variables and instance methods and need to be accessed through instance objects of the class.Classcode{{System.out.println ("Code's Building Block"); }Static{System.out.println ("Code static Block"); }PublicCode () {System.out.println ("Code construction Method"); } }PublicClasscodeblock03{{System.out.println ("CodeBlock03 's Building Block"); }Static{System.out.println ("Static code block for CodeBlock03"); }PublicCodeBlock03 () {System.out.println ("constructor method for 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 The main method code of the static block code of the building block code construction Method Code of the construction block Code construction Method CodeBlock03 construction method of the building block CODEBLOCK03 Co Construction method of construction block CodeBlock03 of deBlock03 * /
Common code blocks in Java, building blocks of code, static code block differences, and code examples (go from sophine)