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
<span style= "FONT-SIZE:12PX;" >//Common code block: {} that appears in a method or statement is called a normal block of code. Ordinary blocks of code and general execution order of statements are determined by the order in which they appear in the code--"first appears first executed" public class codeblock01{public static void Main (string[] args) { { int x=1; System.out.println ("1, variable x= in normal code block" +x); } int x=2; SYSTEM.OUT.PRINTLN ("variable x= within the Main method" +x); { int y=3; System.out.println ("2, variable y= in normal code block" +y); }} /* Run Result: 1, the variable within the normal code block x=1 the variable in the Main method x=2 2, the variable y=3 within the normal code block */</span>
2 Building Code blocks
<span style= "font-size:10px;" >//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. public class codeblock02{ { System.out.println ("first code block"); } Public CodeBlock02 () { System.out.println ("constructor method"); } { System.out.println ("second building Block"); } public static void 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>
3 static code block
<span style= "font-size:10px;" >//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. Class Code{{System.out.println ("Code building Block"); } static{System.out.println ("code static Block"); } public code () {System.out.println ("code construction Method"); }} public class codeblock03{{System.out.println ("CODEBLOCK03 building Block"); } static{System.out.println ("CodeBlock03 Static code block"); } public CodeBlock03 () {System.out.println ("construction 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 construction method CThe construction method of the construction block code of ODE CodeBlock03 The construction method of the building block CodeBlock03 CodeBlock03 The construction method of the construction block CodeBlock03
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java code block, code block, static code block code example and distinguish