Home > Java > javaTutorial > A brief discussion on the initialization process of each member variable in Java

A brief discussion on the initialization process of each member variable in Java

无忌哥哥
Release: 2018-07-19 11:48:58
Original
1706 people have browsed it

Preface

Recently I am reading Thinking in java, and I have never paid attention to the initialization of each member in the class. I will record it here in case there is any error. , please correct me:

1. Inheritance and initialization

Understand the entire initialization process including inheritance, so as to have a global view of everything that happens The grasp is very beneficial.

In Java, the compiled code of each class exists in its own independent file (ie, class file), which is only loaded when the program code is needed. Generally speaking, "the code of the class is loaded when it is used for the first time", which usually includes the following two situations:

  1. When the first object of the class is created

  2. When accessing static methods or fields

Note: When a class is used for the first time, it is also where static initialization occurs. All objects and codes are initialized in the order in which they are written. , fields defined as static will only be initialized once.

Without further ado, the details are as follows:

public class Base {
    //static字段
    private static int I1 = printInt("Init I1");    //普通字段
    private int i = 11;    
    protected int j;    
    public Base(){
        System.out.println("Base constructor");
        System.out.println("i="+i+" j="+j);
        j = 40;
    }    static int printInt(String str) {
        System.out.println(str);        
        return 10;
    }
}
public class Son extends Base{
    private static int I2 = printInt("Init I2");    
    public Son() {
        System.out.println("Son constructor");
        System.out.println("I2="+I2+" j= "+j);
    }    
    public static void main(String[] args) {
        Son son = new Son();
    }
}
Copy after login

After reading this code, what do you think the output is?

Give the answer first, and then analyze it:

Init I1
Init I2
Base constructori=11 j=0Son constructorI2=10 j= 40
Copy after login

Analysis:

The program first starts with the main method of the Son class, so the loader starts to start and find out the Son compilation Code (i.e. class file). During the loading, we found that Son also has a parent class Base, so we continue to load the compiled code of Base (if Base still has a parent class, continue to execute upward). Next, the static field of the root base class is initialized, because the subclass may depend on the base class. Whether the members can be initialized correctly, so Init I1 happened, and then went down to Son, the static field of the Son class was initialized, so Init I2 happened, so far all the necessary After the class loading is completed, you can start initializing the object. Seeing the line of code Son son = new Son(), we are ready to call Son's constructor. We know that in the inheritance relationship, super() will be called in the constructor of the subclass. Of course, this is implicit transfer. This will return to the parent class, but before completing the constructor, all ordinary fields (i.e. non-static fields) in the parent class (Base) will complete their own initialization, so you will see the output i=11 j=0, then come to the subclass (Son), the execution process is the same as the parent class, first complete the initialization of the ordinary fields, and then call the constructor method.

After talking a lot, the overall process is as follows:

  1. Start from the program entry and load the class (here set to Z class), If there is an inheritance relationship, recurse upward until the root class (assuming class A here).

  2. Complete the static field initialization of class A and recurse downward until class Z is encountered.

  3. Complete the common domain initialization of class A, complete the constructor of class A, and recurse downward until class Z is encountered.

The above is the detailed content of A brief discussion on the initialization process of each member variable in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template