[Interview Questions] Java class initialization and instantiation and other important test sites

Foreword:

This article uses a classic Java interview question to explain the initialization and instantiation of Java classes and instance objects. The main content has the following three points:

1. Class initialization process (first)

2. The instance initialization process (after)

3. Method rewriting (note)

First look at the topic and analyze what the output is:

father: 

public class Father {
    private int i = test();
    private static int j = method();
    static {
        System.out.println("(1)");
    }
    Father() {
        System.out.println("(2)");
    }

    {
        System.out.println("(3)");
    }

    public int test() {
        System.out.println("(4)");
        return 1;
    }
    public static int method() {
        System.out.println("(5)");
        return 1;
    }
}

Subclass: 

public class Son extends Father {
    private int i = test();
    private static int j = method();
    static {
        System.out.println("(6)");
    }
    Son() {
        System.out.println("(7)");
    }
    {
        System.out.println("(8)");
    }
    public int test() {
        System.out.println("(9)");
        return 1;
    }

    public static int method() {
        System.out.println("(10)");
        return 1;
    }
    //TEST
    public static void main(String[] args) {
        Son son  = new Son();
        System.out.println();
        Son son1 = new Son();
    }

}

 1. Class initialization process

(1) The class where the main method main is located must be loaded and initialized (even if there is no content in main)

(2) When creating an instance object, the initialization class will be loaded first

(3) A subclass initialization needs to initialize its parent class first 

(4) Class initialization is actually the implementation of <clinit> () method, which consists of static class variables and static code blocks , the order of execution from top to bottom.

      In summary, first the class where the main method main () is located is loaded and initialized (1), and the parent class (3) is loaded and initialized before loading this subclass, which is the static class variable j = method () 【 Output 1], static code block [Output 5], executed from top to bottom; after the parent class is loaded, start loading Son ’s static class variable j = method () [Output 10], static code block [Output 6], from above Go to the next step (4).

2. Instance initialization process

(1) The instance initialization is actually to execute the <init> () method. There is only one class for <clinit>, but there are as many constructors as there are in <init> (may have multiple constructors with and without parameters) . It consists of three parts: static class variables , static code blocks and constructors . Among them, the execution order of non- static instance variables and non-static code blocks is from top to bottom, and the constructor is executed last.

(2) The first line of the <init> () method is super (), whether it is written or not, it exists.

In summary, the subclass instantiation method is divided into the following four steps:

  • 1. Super () => instantiation of parent class (again these three steps)
  • 2. Subclass non-static instance variables, non-static code block execution order from top to bottom
  • 3. Subclass constructor

1. The first is super (), the parent class is instantiated, and the non-static variable i = test, but here involves method rewriting, the default calling object before the non-static method is this , and this refers to the object being created , That is, the subclass object, so i = test outputs the test method in the subclass: [output 9]; followed by the parent class non-static code block: [output 3], then the parent class constructor last: [output 2 】

2. The non-static variable i = test method of the subclass: [output 9]; the non-static code block of the subclass: [output 8]; executed sequentially from top to bottom

3. The last subclass constructor: [Output 7]

The second Son object is created, and again, output [932987], so the total output bit [1 5 10 6 9 3 2 9 8 7 9 3 2 9 8 7]

3. About method rewriting

If the subclass rewrites the parent class method, the code that the subclass has called must be the code rewritten by the subclass. You may wonder why the method test () is overridden by the subclass and the method method is not overridden Then, what method can not be rewritten ?

(1) The final method cannot be rewritten

(2) Static methods cannot be rewritten

(3) Invisible methods in subclasses such as private cannot be overridden

Published 20 original articles · won 15 · views 216

Guess you like

Origin blog.csdn.net/qq_37414463/article/details/105460340