In Java, the code blocks are called code blocks, and the code blocks can be divided into the following four types:
1. Introduction
1. Normal code block:
Method body of method in class
2. Construct the code block :
The constructor is called when the object is created, and it is called every time it is created, taking precedence over the execution of the class constructor.
3. Static code block:
A code snippet wrapped in static{} will only be executed once. Static code blocks take precedence over construction block execution.
4. Synchronize the code block:
Using synchronized(){} code blocks wrapped in, in a multi-threaded environment, read and write operations on shared data need to be mutually exclusive, otherwise it will lead to data inconsistency. Synchronous code blocks need to be written in the method.
2. Similarities and differences between static code blocks and constructed code blocks
Similarities: All JVMs load the class and execute before the constructor is executed. Multiple can be defined in the class. Generally, some static variables are assigned in the code block.
Difference: Static code blocks are executed before non-static code blocks. Static code blocks are executed only once on the first new and are not executed afterwards. Instead, non-static code blocks are executed once every new.
Three. Example
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 Test { public static void main(String[] args) { { int x = 3; System.out.println("Variable x=" + x); } int x = 1; System.out.println("Variable x=" + x); { int y = 7; System.out.println("Variable y=" + y); } } } /* * Run result Variable x=3 in the ordinary code block * Variable x=1 in the main method * Variable y=7 in the ordinary code block */Construct code block: A code block that is directly defined in the class and does not have a static keyword is called a {} construct 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. If there are multiple constructed code blocks, the execution order is determined by the order in which they appear in the code, first appear first execution.
public class Test1 {{System.out.println("first building block");}public Test1(int i) {System.out.println("th" + i + "sub-call" + "constructor");}{System.out.println("second building block");}public static void main(String[] args) {new Test1(0);new Test1(1);new Test1(2);}}/* * Execution result first building block* Second building block* 0th call constructor* First building block* Second building block* First building block* First building block* First building block* Second building block* The second call to construct method*/Static code block: A code block declared using the 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 executed first, then the defined code will be executed later.
Notice:
1. Static code blocks cannot exist in any method body.
2. Static code blocks cannot directly access instance variables and instance methods, and need to be accessed through the instance object of the class.
public class Test3 {public static String STATIC_FIELD = "static attribute";// Static block static {System.out.println(STATIC_FIELD);System.out.println("static code block 1");}public String field = "non-static attribute";// Non-static block {System.out.println(field);System.out.println("Non-static code block 2");}public InitOderTest() {System.out.println("No parameter constructor");}public static void main(String[] args) {InitOderTest test = new InitOderTest();}// Non-static block {System.out.println(field);System.out.println("Non-static code block 1");}// Static block static {System.out.println(STATIC_FIELD);System.out.println("static code block 2");}}/* * Run result static attribute* Static code block 1 * Static attribute* Static code block 2 * Non-static attribute* Non-static code block 2 * Non-static attribute* Non-static code block 1 * Non-argument constructor*/The following code demonstrates the priority relationship between the individual code blocks of creating an object and calling a method:
public class Person {static{System.out.println("1. I am a static block, which takes precedence over the construction block execution! And it only executes once when the first object is created!");}{System.out.println("2. I am a constructor, which takes precedence over the construction method execution! Execute once every object is created!");} public static void function1(){System.out.println("I am a normal code block in a non-static method, which takes precedence when the method is called!");} public static void function2(){System.out.println("I am a normal code block in a static method. The method is executed later than the static block!");}}Test class:
public class HelloWrold {public static void main(String[] args) {new Person().function1();new Person().function1();System.out.println("=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================Running results:
We can see that static blocks are always executed first, and will only be executed once when the first instance of the class is created; the second one is the constructor block; and the third one is the constructor method.
Summarize
The above is all about the detailed explanation of the four code blocks in Java, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!