Four of the members of the class: code block

Code block

  1. The role of code blocks: initialize classes, objects
  2. Can only be modified with static or not modified
  3. Divided into static code blocks and non-static code blocks
  4. The static code block can be executed with the loading of the class. Function: initialize the information of the class
  5. 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
  6. Static code block execution (class loading import) takes precedence over non-static code block (object creation)
  7. When creating an object, the non-static code block runs before the constructor
class blockTest {
    
     
    {
    
    
        System.out.println("这是一个非静态代码块");
    }

    static {
    
    
        System.out.println("这是一个静态代码块");
    }
}    
    ```
    

Guess you like

Origin blog.csdn.net/AmorFati1996/article/details/108714951