Source: https://liuyanzhao.com/5303.html
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.
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 .
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.
Execution order of static code blocks, construction code blocks, and constructors at the same time: static code blocks > constructs code blocks > Constructors ;
Let's give a few examples to illustrate everything.
Demo1: Construction method
- Public class Test {
- Static {
- System.out.println ("static Block");
- }
- {
- System.out.println ("building blocks, defined in class");
- }
- Public Test () {
- System.out.println ("construction Method Execution");
- }
- public static void Main (string[] args) {
- new Test ();
- new Test ();
- }
- }
- /*
- Static block
- Constructs a block, defined in a class
- Construction method Execution
- Constructs a block, defined in a class
- Construction method Execution
- */
Demo2: Normal code block
- /* 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 Occurrence first" */
- Public class Test {
- public static void Main (string[] args) {
- {
- int x = 3;
- System.out.println ("1, variable x= within the normal code block" + x);
- }
- int x = 1;
- SYSTEM.OUT.PRINTLN ("variable x= within the Main method" + x);
- {
- int y = 7;
- System.out.println ("2, variable y= within the normal code block" + y);
- }
- }
- }
- /*
- 1, variable x=3 in normal code block
- Variable X=1 within the Main method
- 2, variable y=7 in normal code block
- */
DEMO3: Building Blocks of code
- /* 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.
- Constructs a block of code that is called when the object is created, and is called every time the object is created.
- and the execution order of the Construction code block takes precedence over the class constructor. */
- Public class Test {
- {
- System.out.println ("first code block");
- }
- Public Test () {
- System.out.println ("construction Method");
- }
- {
- System.out.println ("second building block");
- }
- public static void Main (string[] args) {
- new Test ();
- new Test ();
- new Test ();
- }
- }
- /*
- First block of code
- Second building block
- Construction method
- First block of code
- Second building block
- Construction method
- First block of code
- Second building block
- Construction method
- */
Demo4: 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. */
- Class Code {
- {
- System.out.println ("code building Block");
- }
- Static {
- System.out.println ("code static Block");
- }
- Public Code () {
- System.out.println ("code Method of Construction");
- }
- }
- Public class Test {
- {
- System.out.println ("Test's building Block");
- }
- Static {
- System.out.println ("Static code block for Test");
- }
- Public Test () {
- System.out.println ("test method of construction");
- }
- public static void Main (string[] args) {
- System.out.println ("The Main method of test");
- new Code ();
- new Code ();
- new Test ();
- new Test ();
- }
- }
- /*
- Static block of code for test
- The main method of test
- Code static block
- Code's building block
- How code is constructed
- Code's building block
- How code is constructed
- Test's construction block
- How to construct test
- Test's construction block
- How to construct test
- */
The difference between a static block of code in Java and a block of code construction