Code blocks: In Java, code enclosed in {} is called a code block.
Depending on its location and declaration, it can be divided into
Local code block: a local location that defines the life cycle of a variable.
Construct code block: The position of the member in the class, with the code enclosed in {}. The construction code block is executed before each call to the construction method execution.
Function: You can put together the common code in multiple construction methods to initialize the object.
Static code block: The position of a member in a class, with the code enclosed in {}, except that it is decorated with static.
Function: The class is typically initialized.
Static blocks of code, building blocks of code, constructing the order in which the methods are executed?
Execute static code block first
Then execute the construction code block
Last point to construct method
Class fu{ static { System.out.println ("Fu Static Code"); } { System.out.println ("Fu Code"); } Public Fu () { System.out.println ("Fu Gouzao");} } Class Zi extends fu{ static { System.out.println ("Zi Static Code"); } { System.out.println ("Zi Code"); } Public Zi () { System.out.println ("Zi Gouzao");} } public class text{public static void Main (string[] args) { Zi Zi = new Zi ();} }
Output:
Fu static Codezi static codefu Codefu Gouzaozi Codezi Gouzao
code block execution order in Java