The code copy is as follows:
public class Derive extends Base
{
private Member m1 = new Member("Member 1");
{
System.out.println("Initial Block()");
}
public Derive() {
System.out.println("Derive()");
}
private Member m2 = new Member("Member 2");
private int i = getInt();
private int getInt()
{
System.out.println("getInt()");
return 2;
}
public static void main(String[] args)
{
new Derive();
}
}
class Base
{
public Base()
{
System.out.println("Base()");
}
}
class Member
{
public Member(String m)
{
System.out.println("Member() "+m);
}
}
/*Program output:
Base()
Member() Member 1
Initial Block()
Member() Member 2
getInt()
Derive()
*/
The conclusion is as follows: without considering the initialization of static members, when calling an object's constructor, the program first calls the constructor of the parent class (the constructor of the parent class can be specified through the super keyword, otherwise the constructor without parameters will be called by default. And it needs to be called on the first line of the constructor of the subclass), and then the initialization function and static initialization block of the static member variable are executed in the order in the code. If the member variable does not have a specified value, the default value is assigned, that is, the basic data The type is 0 or false, etc., and the object is null; finally call the own constructor.