Code block
- The role of code blocks: initialize classes, objects
- Can only be modified with static or not modified
- Divided into static code blocks and non-static code blocks
- The static code block can be executed with the loading of the class. Function: initialize the information of the class
- The non-static code block is executed with the creation of the object, and its role is to initialize the properties of the created object
- Static code block execution (class loading import) takes precedence over non-static code block (object creation)
- When creating an object, the non-static code block runs before the constructor
class blockTest {
{
System.out.println("这是一个非静态代码块");
}
static {
System.out.println("这是一个静态代码块");
}
}
```