Loading of JAVA Objects and Initialization of Objects

Links to the original text: https://my.oschina.net/u/2385466/blog/547062

First, the initialization order of the object: (the order in which the java class loader loads the class:)
(1) Loading the parent class (the following serial numbers are the same, indicating that initialization is in the top-down order of the code)
1. Allocate space for static attributes of parent class and assign initial values
1. Execute static initialization block of parent class;
(2) Loading subclasses
2. Allocate space for static attributes of subclasses and assign initial values
2. Execute static content of subclasses;
(3) Loading the parent constructor
3. Initialize the non-static attributes of the parent class and assign them to the initial value
3. Executing non-static code blocks of parent classes;
4. Execute the construction method of the parent class;
(4) Loading subclass constructor
5. Initialize non-static attributes of subclasses and assign them to initial values
5. Non-static code blocks that execute subclasses;
6. Executing the construction method of subclasses.
In a word, the content of the static code block is executed first (parent followed by son), then the non-static code block and construction method of the parent class are executed, and then the non-static code block and construction method of the subclass are executed.
2. Initialization order of static variables and static code blocks:
Who initializes first (which is easy to understand, when initializing, it is impossible to jump to initialize, for example, static code blocks in front of static variables, it is impossible to jump over static code blocks to initialize static variables first).
Note: Subclass construction method, regardless of whether the construction method with or without parameters, default it will first look for the parent class without parameters of the construction method. If the parent class does not have a constructor without parameters, then the subclass must be invoked with the supper key child
The parent class is constructed with parameters, otherwise the compilation cannot pass.
Class loading steps
In Java, a class loader loads a class into a Java virtual machine through three steps: loading, linking and initialization. The links can be divided into three steps: checking, preparing and parsing. Besides parsing, the other steps are completed in strict order. The main tasks of each step are as follows:
Loading: Finding and importing binary data of classes or interfaces;
Links: Perform the following steps of validation, preparation and parsing, where the parsing step is optional;
Check: Check the correctness of the binary data of the imported class or interface;
Prepare: Assign static variables to classes and initialize storage space;
Parsing: Converting symbolic references to direct references;
Initialization: Initialization of Java code and static Java code blocks to activate static variables of classes.
Attributes in initialization classes are a common use of static code blocks, but only once.

Object Initialization Sequence Test Code

StaticIniBlockOrderTest
StaticIniBlockOrderTest
class Parent {
    static String name ="hello";
    {
        System.out.println("parent block");
    }
    static {
        System.out.println("parent static block");
    }
    public Parent() {
        System.out.println("parent constructor");
    }
}
class Child extends Parent {
    static String childName ="hello";
    {
        System.out.println("child block");
    }
    static {
        System.out.println("child static block");
    }
    public Child() {
        System.out.println("child constructor");
    }
}
publicclass StaticIniBlockOrderTest {
    publicstaticvoid main(String[] args) {
        new Child();// Statement (*)
    }
}

Operation results:

  parent static block
  child static block
  parent block
  parent constructor   child block   child constructor

Initialization Sequence Test Code for Static Variables and Static Code Blocks

publicclass TestOrder {
//Static variables
publicstatic TestA a =new TestA();

//Static Initiation Block
static {
System.out.println("Static Initiation Block");
}

//Static variables
publicstatic TestB b =new TestB();

publicstaticvoid main(String[] args) {
new TestOrder();
}
}

class TestA {
public TestA() {
System.out.println("Test--A");
}
}

class TestB {
public TestB() {
System.out.println("Test--B");
}
}

Operation results:

  Test--A 
 Static Initiation Block 
   Test--B

Add a classic test code

package statictest;

class insect{
    int i=9;
    int j;

    static {
        prt("static block first,because it's begin of the static variable");
    }

    insect(){
        System.out.println("insect initialized");

       prt("i= "+i+" j="+j);

       j=39;

    }

    static int x1=prt("static insect x1 initialized");

    static int prt(String s){

       System.out.println(s);

       return 47;

    }

}

public class Wps extends insect{

    Wps(){
        System.out.println("wps initialized");

       prt("k="+k);

       prt("j="+j);

    }
    int k=prt("the member k in wps be initialized");

    static int x2=prt("static wps x2 initialized");

    static int prt(String s){

       System.out.println(s);

       return 63;

    } 

public static void main(String[] args){

       insect.prt("initialized constructor");

// Wps w=new Wps();

    }

}

Reproduced in: https://my.oschina.net/u/2385466/blog/547062

Tags: Java

Posted on Thu, 12 Sep 2019 23:17:28 -0700 by RestlessThoughts