Execution order judgment of static code blocks and construction methods in Java

preface

Static code takes precedence over non static code because members modified by static are class members and will be executed when the JVM loads the class. Members not modified by static are also called instance members. Objects need to be created before they are loaded into heap memory. So static will take precedence over non static.

When executing the constructor (construction method), there are three implicit steps before executing the method body:

1. Super statement. The following three situations may occur:

1) If the first line of the constructor body is this statement, the implicit three steps will not be executed,

2) If the first line of the constructor body is a super statement, call the constructor of the corresponding parent class,

3) If the first line of the constructor body is neither this statement nor super statement, it implicitly calls super (), that is, the default constructor of its parent class, which is why a parent class usually provides the default constructor;

2. Initialize non static variables;

3. Construct code blocks.

It can be seen that constructing a code block takes precedence over the method body of the construction method, but the this keyword and the super keyword cannot appear at the same time, and can only appear in the first line of the code. If the this keyword appears, the implicit three steps will not be performed.

First look at the following classes, and then judge their output:

The order in which parent and child classes are executed

Execution characteristics of static variables

Precautions for method override

1. When both parent and child classes have static code blocks and constructors, the execution order is as follows:

Parent static code block > child static code block

Parent constructor > child constructor (father before child)

If it is a multi-level inheritance relationship, the top-level parent class executes first, and then decreases in turn

Summary: static execution first, parent class execution first

Note: the static code block is executed when the JVM loads the class, and the static code block is executed only once

2. When calling a method in a class, first assign a value to the member variable in the class before the method body is executed. If there is no specific value assigned in the code, there is also a default value. The assignment order of member variables is in the order before and after.

If there are both direct assignment and construction method assignment, it shall be executed according to the sequence

3. Override overload

Overloads are method names with the same return types. The only difference is that the method parameters are different (different parameter types or different parameter types)

rewrite:

Polymorphism:

The reference of the parent class points to the object of the child class. The method executed when calling is also the method of the child class. The method of the parent class will not be executed,

The so-called polymorphism is that the reference of the parent class or interface type can point to the child class or the instance (object) of the class implementing the interface,

After reading the above instructions, you should also know the above program output results.

The output result is: 1a2b2b

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>