java class initialization and instance initialization

First define a parent class

/**
 * @author Fbin
 * @date 2020/3/12
 * @annotation If you can't reach the mountain, you will go to it
 */
public class Father {
	//Member variables
    public int i = test();
    //Static member variable
    public static int j = method();
    //Static code block
    static {
        System.out.print("(1)");
    }
    //Construction method of parent class
    public Father() {
        System.out.print("(2)");
    }
    //Code block
    {
        System.out.print("(3)");
    }
    public int test() {
        System.out.print("(4)");
        return 1;
    }
    public static int method() {
        System.out.print("(5)");
        return 1;
    }
}

Next, create a subclass object to inherit the parent class. And create the main method as follows:

/**
 * @author Fbin
 * @date 2020/3/12
 * @annotation If you can't reach the mountain, you will go to it
 */
public class Son extends Father{
    //Member variables
    public int i = test();
    //Static member variable
    public static int j = method();
    //Static code block
    static {
        System.out.print("(6)");
    }
    //Subclass construction
    public Son() {
        System.out.print("(7)");
    }
    //Code block
    {
        System.out.print("(8)");
    }
    public int test() {
        System.out.print("(9)");
        return 1;
    }
    public static int method() {
        System.out.print("(10)");
        return 1;
    }
    public static void main(String[] args) {
    }
}

At this time, execute the main method, and the console prints the following results

Reason:

Calling the main() method will initialize the subclass (calling the < clinit > method)
1, public static int j = method();
2. Static code block of subclass
static {
        System.out.print("(6)");
}

There's another rule

Initialize the parent class first
 Reinitialize subclass


So the current execution order is

1, Static variables of the parent class
public static int j = method();
public static int method() {
     System.out.print("(5)");
     return 1;
}
2,Static code block of subclass
static {
        System.out.print("(1)");
}
3, Static variables of subclasses
public static int j = method();
public static int method() {
     System.out.print("(10)");
     return 1;
}
4,Static code block of subclass
static {
        System.out.print("(6)");
}

The summary is as follows:

1. To create an instance, a class needs to be loaded and initialized first
	The class of main method needs to be loaded and initialized first
 2. A subclass needs to be initialized. The parent class needs to be initialized first
 3. A class initialization is to execute the < clinit > () method
	< clinit > () method consists of static class variable display assignment code and static code block
	Class variable shows that the assignment code and static code block code are executed sequentially from top to bottom
	< clinit > () only once

Now add the following code to the main method of the Son class. At this time, the Son class is as follows

/**
 * @author Fbin
 * @date 2020/3/12
 * @annotation If you can't reach the mountain, you will go to it
 */
public class Son extends Father{
    //Member variables
    public int i = test();
    //Static member variable
    public static int j = method();
    //Static code block
    static {
        System.out.print("(6)");
    }
    //Subclass construction
    public Son() {
        System.out.print("(7)");
    }
    //Code block
    {
        System.out.print("(8)");
    }
    public int test() {
        System.out.print("(9)");
        return 1;
    }
    public static int method() {
        System.out.print("(10)");
        return 1;
    }
    public static void main(String[] args) {
        System.out.println("\n Start creating first instance s1");
        Son s1 = new Son();
        System.out.println("\n Start creating second instance s2");
        Son s2 = new Son();
    }
}

As you can see, there are two instantiations in the main method

Instantiation of subclass:
1,super()First, that is Father class
2,Nonstatic variable of subclass
public int i = test();
public int test() {
    System.out.print("(9)");
    return 1;
}
3,Nonstatic code block of subclass
{
    System.out.print("(8)");
}
4,Nonparametric construction of subclasses (last)
//Subclass construction
public Son() {
    System.out.print("(7)");
}
    
//Instantiation method of parent class
1,super()The most advanced Object Class, not printed here
2,Nonstatic variable of parent class
public int i = test();
//This is actually a call to this. At this time, the test method has been overridden by the subclass, so the subclass is actually called
public int test() {
    System.out.print("(9)");
    return 1;
}
3,Non static code block of parent class
{
    System.out.print("(3)");
}
4,Nonparametric construction of parent class (last)
//Construction method of parent class
public Father() {
    System.out.print("(2)");
}
//So the order of execution is(9)(3)(2)(9)(8)(7)

The results are as follows

summary

1. Instance initialization is to execute the < init > () method
	< init > () there may be multiple overloads, and there are several < init > () methods for several constructors
	The < init > () method consists of non static instance variable display assignment code, non static code block and corresponding constructor code
	The nonstatic instance variable shows that the assignment and nonstatic code block code are executed from top to bottom, while the code corresponding to the constructor is executed last
	Every time an instance object is created, the corresponding constructor is called, and the corresponding < init > () method is executed
	The first line of the < init > () method is super() or super (argument list), that is, the < init > () method corresponding to the parent class
 2. Method override
	1. Which methods cannot be overridden
			final method
			Static method
			The method of private decoration
	2. Polymorphism of objects
			Subclass if the method of the parent class is overridden, the code that must be overridden by the subclass must be called through the subclass object
			The default calling object of non static method is this
			this object is the object being created in the constructor or < init > () method
32 original articles published, praised 4, 778 visitors
Private letter follow

Posted on Thu, 12 Mar 2020 00:50:58 -0400 by EWaveDev