/ Java  

Java Static code block

Static code block: declared with staitc, executed when the jvm loads the class, only once
Constructing 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
2
3
// Constructor
public HelloA(){
}

Regarding the constructor, the following points should be noted:

  1. Once the object is created, the corresponding constructor will be called, that is, if the object is not created, the constructor will not run.
  2. The function of the constructor is used to initialize the object.
  3. 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
2
3
// Construction code block
{
}

With regard to constructing code blocks, the following points should be noted:

  1. The purpose of the construction code block is to initialize the object.
  2. 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.
  3. 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
2
3
// Static code block
static {
}

Regarding static code blocks, note that:

  1. 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.
  2. Static code blocks are actually initialized to classes, while constructing code blocks are initialized to objects.
  3. 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Test{
static int cnt=6;

static{
cnt+=9;
}

public static void main(String[] args) {
System.out.println(cnt);
}

static{
cnt/=3;
}
}

// Results:
// 5

Execution order

For a class, constructor, static block and construction code block are executed in the following order:

  1. Static code block
  2. Construction code block
  3. 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Test {
// Constructor
public Test(){
System.out.println("A's constructor");
}

// Construction code block
{
System.out.println("A's construction code block");
}

// Static block
static {
System.out.println("A's static block");
}

public static void main(String[] args) {
}
}

// Output:
// A's static block

Example 2: Static code block is executed before construction code block and constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Test {
// Constructor
public Test(){
System.out.println("A's constructor");
}

// Construction code block
{
System.out.println("A's construction code block");
}

// Static block
static {
System.out.println("A's static block");
}

public static void main(String[] args) {
Test test = new Test();
}
}

// Output:
// A's static block
// A's construction code block
// A's constructor

Example 3: Static block is only executed once.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Test {
// Constructor
public Test(){
System.out.println("A's constructor");
}

// Construction code block
{
System.out.println("A's construction code block");
}

// Static block
static {
System.out.println("A's static block");
}

public static void main(String[] args) {
Test test = new Test();
Test test2 = new Test();
}
}

// Output:
// A's static block
// A's construction code block
// A's constructor
// A's construction code block
// A's constructor

When it comes to inheritance, the order is as follows:

  1. The static code block of the parent class and initialize the static member variables of the parent class
  2. The static code block of the subclass and initialize the static member variables of the subclass
  3. The parent class’s construction code block, execute the parent’s constructor, and initialize the ordinary member variables of the parent class
  4. The construction code block of the subclass, execute the constructor of the subclass, and initialize the ordinary member variables of the subclass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class TestA {
// Constructor
public TestA(){
System.out.println("A's constructor");
}

// Construction code block
{
System.out.println("A's construction code block");
}

// Static block
static {
System.out.println("A's static block");
}
}

public class TestB extends TestA{
// Constructor
public TestB(){
System.out.println("B's constructor");
}

// Construction code block
{
System.out.println("B's construction code block");
}

// Static block
static {
System.out.println("B's static block");
}

public static void main(String[] args) {
TestB testB = new TestB();
}
}

// Output:
// A's static block
// B's static block
// A's construction code block
// A's constructor
// B's construction code block
// B's constructor