Code block definition

In Java, code enclosed in {} is called a code block.

Classification of code blocks

Depending on its location and declaration:

Interview question: Order of execution of constructors and static blocks

In the classification description above, we have given the answer; Let’s run around testing the code and verify these answers against the results of the code execution

No inheritance

package com.milo.java7.codeblock;

/ * * *@author Milo Lee
 * @dateThe 2021-04-23 15:43 * /
public class People {

    {
        System.out.println("Superclass construction code block");
    }

    static {
        System.out.println("Superclass static code block");
    }

    public People(a) {
        System.out.println("Parameterless constructor for parent class construction");
    }
    public People(String str){
        System.out.println("Parent parameterized constructor");
    }

    public static void main(String[] args) {
        newPeople(); }}Copy the code

Execution Result:

Now let’s change the code to see what happens to the result of execution with parameters.

 public static void main(String[] args) {
        new People("Milo");
    }
Copy the code

Execution Result:

We find that the parameterless constructors become parameterized constructors, which is quite understandable

Now let’s take a look at what happens when we have multiple objects.

It is important to note that the static code block is executed only once; Wechat search “Love to write bugs Milo “, follow me, work together to write bugs

Have a hereditary relationship

Let’s create a Student class that inherits from People

package com.milo.java7.codeblock;

/ * * *@author Milo Lee
 * @dateThe 2021-04-23 it * /
public class Student extends People {
    {
        System.out.println("Subclass construction code block");
    }
    static {
        System.out.println("Subclass static code block");
    }

    public Student(a) {
        //super(null);
        System.out.println("Subclass construction code block");
    }

    public static void main(String[] args) {
        newStudent(); }}Copy the code

Execution Result:

If we replace it with a parameter, let’s look at the result:

Now let’s take a look at what happens when we have multiple objects.

In the example above, we found that all calls to the parent class have no arguments constructor, so when to call the parent class with arguments constructor?

First, let’s change the code

Student

// no argument constructor
public Student(a) {
        super(null);
        System.out.println("Subclass parameterless constructor");
    }
    
  // Test method
  public static void main(String[] args) {
        new Student();
    }
Copy the code

You see, that’s how it works

The role of a code block

With so many code blocks, are you as curious as I am about their application scenarios?

Plain code block

public void show(a){
    {
        System.out.println("Normal code block run!"); }}Copy the code

Some places are also called local code blocks. In a program, when we define a local variable x and we don’t want to use it in the rest of the code, there is no need for x to take up space in memory. Hence the local code block

Construct code block

The Body of the class definition initializes the partial fields of the class uniformly:

public class Apple {
    private String size;

    // Construct the code block
    {
        System.out.println("Construct code block to run!");
        size = "E"; }}Copy the code

: is the difference between tectonic blocks of code and the constructor structure block of code is for all objects unity initialization, and the constructor is to give the corresponding object initialization, because the constructor can be multiple, which the constructor will run to establish what kind of object, but no matter which object, will perform the same tectonic blocks of code first. That is, the initialization content that is common to different objects is defined in the construction code block. So it stands to reason that the constructor code block is executed before the constructor.

Static code block

(1) Assign a value to a static variable of a class; (2) Declare static variables;

Class Body to initialize a static variable in the class:

public class APP {
    static int x, y; // Static variables

    static {
        x = 5; // Assign to the static variable x
    }

    public static void myMethod(a) {
        y = x++ + ++x; // x++ uses the value of x plus 1; Plus plus x plus 1 before we use the value of x
    }

    public static void main(String[] args) { x--; myMethod(); System.out.println(x + y + ++x); }} output:23
Copy the code

Note that if the code in the static code block is changed to:

static {
    int x = 5; // Declare a static variable
}
Copy the code

The static variable is declared and scoped only in the code block, not initialized. The final output is 3.

Synchronized code block

We call the statement blocks modified with the synchronized modifier as synchronized code blocks, which are mainly used to solve the security problems in multithreading

synchronized(Object o ){}
Copy the code

Now that you understand what they do and how to use them, that’s all for today’s sharing. Thank you