Initialization block of jvaa
1. Function of initialization block: initialize Java objects;
2. Program execution sequence: declare the default value of member variables -- > display initialization, and multiple initialization blocks are executed in turn (executed in sequence at the same level) - > the constructor is assigning values to members.
Output:
Non static code block 1 is executed
Non static code block 2 is executed
The construction method is executed
3. Static code block: when a class is loaded, the class attribute declaration and static code are executed in fast sequence and only once; Static code blocks can only use static modified attributes and methods;
Output:
Static code block 1 is executed, static code block 2 is executed, non-static code block 1 is executed, non-static code block 2 is executed, construction method is executed, non-static code block 1 is executed, non-static code block 2 is executed, construction method is executed
Note: each time an object is new, the static code block is executed, while the static code block is executed only once.
Non static code block:
Static code block: a code block decorated with static