Constructor, static code block, construction code block, common code block in Java

Time:2020-3-27

1. Constructor

1) The name of the constructor must be exactly the same as the class name. A class can have multiple constructors with different parameters (in Java, the common function can have the same name as the constructor, but it must have a return value);

2) The function of the constructor is mainly used to define the initialization state when the class’s object is created. It has no return value and cannot be decorated with void. This ensures that it not only does not have to return automatically, but also has no choice at all. Other methods have return values, even void. Although the method body itself does not automatically return anything, it can still return something, which may not be safe;

3) Constructors can’t be called directly. They can only be called automatically when an object is created by the new operator, while general methods are called when the program reaches it;

4) When defining a class, the constructor of the class is usually displayed, and the initialization work specified in the function can also be omitted, but the java compiler will provide a default constructor. This default constructor has no parameters. The general method does not have this characteristic;

public class Code {
    public Code() {
        System. Out. Print ("parameterless constructor \ n");
    }
    public Code(String name) {
        System. Out. Print (name + "parameterconstructor \ n");
    }
}

The constructor will execute the corresponding constructor every time the object is instantiated;

@Test
void codeTest() {
    Code code = new Code();
    Code code2 = new Code("java");
}

Print:

Constructor
java有参Constructor

 

2. Static code block

Code block defined in class with static and {}:

public class Code {
    public Code(String name) {
        System. Out. Print (name + "parameterconstructor \ n");
    }

    static {
        System. Out. Print ("static code block \ n");
    }
}

The static code block will be executed only once when the class is loaded, so it cannot exist in any method block;

@Test
void codeTest() {
    Code code = new Code("java");
    Code code2 = new Code("springboot");
}

Print:

Static code block
Java parameter constructor
Springboot parameterconstructor

 

3. Construct code block

The code block defined with {} in the class is less static than the static code block above

public class Code {
    static {
        System. Out. Print ("static code block \ n");
    }

    {
        System. Out. Print ("building block \ n");
    }

    public Code() {
        System. Out. Print ("parameterless constructor \ n");
    }

    public Code(String name) {
        System. Out. Print (name + "parameterconstructor \ n");
    }
}

Every time an object is instantiated, the construction code block is executed before the constructor;

@Test
void codetest() {
    Code code = new Code("java");
    Code code2 = new Code("springboot");
}

Print:

Static code block
Building blocks
Java parameter constructor
Building blocks
Springboot parameterconstructor

 

4. Common code block

The code block defined with {} in the method is executed when the method is called in the same order as the code

public class Code {
    public void fun(String str) {
        {
            System. Out. Print ("normal code block \ n");
        }
    }
}

 

5. Execution sequence

Static code block > construct code block > constructor > normal code block

 

6.Parent and child execution order

Parent static code block > subclass static code block > parent construct code block > parent constructor > subclass construct code block > subclass constructor

Reference: https://www.cnblogs.com/ysocean/p/8194428.html × label1 × 0