Code block: Code enclosed in Java with {}
(1) Code enclosed in {} in Java.
(2) Code block classification: (depending on its location and declaration)
A: Local code block
In the method definition, it is used to qualify the life cycle of a variable, release it early, and improve memory utilization.
B: Building Blocks of code
Appearing outside of a method in a class (that is, in a member position in a class), you can put together the same code in multiple construction method methods to initialize the object, execute each call to the construction method, and execute before the method is constructed.
C: Static code block
Occurs outside of a method in a class (that is, a member position in a class), with the static modifier used to initialize the class, which executes at the time the class is loaded and executes only once.
D: Synchronizing code blocks
Multi-threaded section explained.
(3) Interview questions:
Static block of code, Construction code block, the execution order of the construction method problem?
Static code blocks > Constructing code blocks > Construction Methods
Executes only once per call construction method
As shown in the following:
Code block: Code enclosed in Java with {}