Java class initialization and instance initialization

Time:2022-5-20

Java class initialization and instance initialization

Class initialization process

A class initialization is execution()Method, which is composed of static variable explicit assignment code and static code block

The static variable explicit assignment code and static code block are executed from top to bottom

()Method is executed only once

Subclass initialization needs to initialize the parent class first

Instance initialization process

Instance initialization is execution()Method, which may overload multiple constructors()method

()Method consists of non static instance variable explicit assignment code, non static code block and corresponding constructor code

The non static instance variable explicit assignment code and non static code block are executed from top to bottom, and the corresponding constructor code is executed last

()The first line of the method is super () or super (argument list), which corresponds to the parent class()Method, so execute the subclass()Method will execute the parent class first()Method, to be frank: the constructor of the parent class must be called in the subclass constructor

If super () writes or does not write, there will be the first line of the constructor. The class needs to be loaded and initialized before instance initialization

Code practice

Parent class

package com.yl;

/**
 *Parent class
 *
 * @author Y-wee
 */
public class Father {
    private int i=getI();
    private static int j=getJ();

    static {
        System.out.print("-1-");
    }

    {
        System.out.print("-3-");
    }

    public Father(){
        System.out.print("-2-");
    }

    public int getI() {
        System.out.print("-4-");
        return 1;
    }

    public static int getJ(){
        System.out.print("-5-");
        return 1;
    }

}

Subclass

package com.yl;

/**
 *Subclass
 *
 * @author Y-wee
 */
public class Son extends Father {
    private int i = getI();
    private static int j = getJ();

    static {
        System.out.print("-6-");
    }

    {
        System.out.print("-8-");
    }

    public Son() {
        System.out.print("-7-");
    }

    @Override
    public int getI() {
        System.out.print("-9-");
        return 1;
    }

    public static int getJ() {
        System.out.print("-10-");
        return 1;
    }

    public static void main(String[] args) {
        Son sonA = new Son();
        System.out.println();
        Son sonB = new Son();
        System.out.println();
    }
}

Output result:

Resolution:

  • The son class will be loaded and initialized before the Sona instance is initialized

  • Since the son class inherits the father class, the father class will be loaded and initialized first

  • It can be seen from the class initialization process that 5 and 1 are output first

  • After the father class is initialized, the son class is initialized. The output is 10 and 6

  • After the initialization of both the subclass and the parent class is completed, the instance initialization starts

  • The parent class instance initialization will be performed before the child class instance initialization

  • From the instance initialization process, execute the explicit assignment code of non static instance variable of father class first:

    private int i=getI();

In fact, there is a default object this in front of the non static method. This is in the constructor or()The method represents the object being created. Because the son object is being created here, the Geti () method of the subclass is executed

Some people may have questions here. The subclass also rewrites the getj () method of the parent class. Why does the parent class execute the getj () method of the subclass when initializing? Because invisible methods in subclasses such as final method, static method and private cannot be rewritten, so the subclass does not rewrite the getj () method of the parent class. The two methods are different

  • Therefore, the initialization of the parent class instance is completed, and the output is 9, 3 and 2
  • Subclass instance initialization is completed, output: 9, 8, 7
  • Sonb instance initialization. Since the class is initialized only once, there is no need to perform the initialization process of father class and son class
  • Sonb instance initialization will also execute parent instance initialization first. The output is 9, 3 and 2
  • After the parent class instance initialization is completed, execute the sonb instance initialization. The output is 9, 8, and 7

Recommended Today

The five-year itch of software test engineers tells the experience of breaking through bottlenecks in the past two years

Breaking through your own technical bottleneck is not an overnight achievement. You still need to see something clearly. Here are some experiences and insights to share with you. As a professional, I also have my experience and story. During my work, I had a small “record” of raising wages seven times in two years (there […]