Static code block
: declared with staitc
, executed when the jvm loads the class, only onceConstructing code blocks
: defined by {}
directly in the class and is executed every time an object is created.Execution order
: static block, main(), function, constructing block, constructor.
Constructor
1 | // Constructor |
Regarding the constructor, the following points should be noted:
- Once the object is created, the corresponding constructor will be called, that is, if the object is not created, the constructor will not run.
- The function of the constructor is used to initialize the object.
- To create an object, the constructor is run only once, and normal methods can be called multiple times by the object.
Construction code block
1 | // Construction code block |
With regard to constructing code blocks, the following points should be noted:
- The purpose of the construction code block is to initialize the object.
- The construction block is run as soon as the object is created, and it takes precedence over the execution of the constructor. It should be emphasized here that only when an object is created, the construction code block can be run. The class cannot call the construction code block, and the execution order of the construction code block and the constructor is that the former is executed before the latter.
- The difference between a construction code block and a constructor is that a construction code block is a uniform initialization of all objects, and a constructor is a corresponding object initialization, because there can be multiple constructors, whichever constructor is run will create what Object, but no matter which object is created, the same building code block is executed first. In other words, what is defined in the construction code block is the initialization content common to different objects.
Static code block
1 | // Static code block |
Regarding static code blocks, note that:
- It is executed as the class loads, only once, and takes precedence over the main function. Specifically, static code blocks are called by classes. When the class is called, the static code block is executed before the main function is executed.
- Static code blocks are actually initialized to classes, while constructing code blocks are initialized to objects.
- Variables in static code blocks are local variables, and they are no different from local variables in ordinary functions.
There can be multiple static code blocks in a class
Example:
1 | public class Test{ |
Execution order
For a class, constructor, static block and construction code block are executed in the following order:
- Static code block
- Construction code block
- Constructor
Example 1: Static block is called when JVM loads the class. Even if not object of the class is defines, it will be called.
1 | public class Test { |
Example 2: Static code block is executed before construction code block and constructor
1 | public class Test { |
Example 3: Static block is only executed once.
1 | public class Test { |
When it comes to inheritance, the order is as follows:
- The static code block of the parent class and initialize the static member variables of the parent class
- The static code block of the subclass and initialize the static member variables of the subclass
- The parent class’s construction code block, execute the parent’s constructor, and initialize the ordinary member variables of the parent class
- The construction code block of the subclass, execute the constructor of the subclass, and initialize the ordinary member variables of the subclass
1 | public class TestA { |