Preface
Static code takes precedence over non-static code because the members modified by static are all class members and will be executed as the JVM loads the class. Members not modified by static are also called instance members. Objects are needed to be created before they can be loaded into heap memory. Therefore, static will be preferred for non-static.
When executing the constructor (construction method), there are three implicit steps before executing the method body:
1. The super statement may have the following three situations:
1) The first line of the constructor is this statement, and the implicit three steps will not be executed.
2) The first line of the constructor body is a super statement, and the constructor of the corresponding parent class is called.
3) The first line of the constructor body is neither this statement nor a super statement, and super() is called implicitly, that is, the default constructor of its parent class, which is why a parent class usually needs to provide a default constructor;
2. Initialize non-static variables;
3. Construct the code block.
From this we can see that the construction code block takes precedence over the method body of the construction method, but this keyword and the super keyword cannot appear at the same time, and can only be on the first line of the code. If this keyword appears, the implicit three steps will not be executed.
First look at the following classes and then judge their output:
public class A { static{ System.out.print(1); } public A(){ System.out.print(2); } } public class B extends A{ static{ System.out.print("a"); } public B(){ System.out.print("b"); } } public class C { public static void main(String[] args){ A a = new B(); a = new B(); } } The order of execution of parent class and child class
Execution characteristics of static variables
Notes on method override
1. When both the parent and child classes have static code blocks and constructors, the execution order is as follows:
Parent class static code block > Subclass static code block
Parent class constructor > Subclass constructor (first there is a father, then there is a child)
If it is a multi-level inheritance relationship, the parent class at the highest level will be executed first, and then decremented in turn.
Summary: Static execution is preferred, parent class execution is preferred
Note: Static code blocks are executed when the JVM loads the class, and the static code blocks are executed only once.
2. When calling a method in a class, before executing the method body, the member variables in the class must be assigned first. If no specific value is assigned in the code, there is also a default value. The assignment order of member variables is carried out in the order before and after.
If there are both direct assignments and constructor assignments, then execute them in sequence
3. Override (Override) Overload (Overload)
Overloading is the method name, the return type is the same, 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 subclass. The method executed when invoked is also the method of the subclass. The method of the parent class will not be executed.
The so-called polymorphism means that the reference of the parent class or interface type can point to a subclass or an instance (object) of the class that implements the interface.
After reading the above instructions, you should also know the output results of the above program.
The output result is: 1a2b2b
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.