Object instantiation process

Object instantiation process:

1. Check whether the class has been loaded. If not, initialize the class first.

2. Allocate space in heap memory.

3. Initialize the properties of the parent class

4. Construction method of initialization parent class

5. Initialize the properties of the subclass

6. Construction method of initialization subclass

Example:

package com.xm.load;

public class Animal {

    static String str = "I`m a animal!";

    public String str1 = "I`m 10 years old @Animal";

    static {
        System.out.println(str);
        System.out.println("Load parent static code block");
    }

    static void doing() {
        System.out.println("This ia a Animal!");
    }

    public Animal() {
        doing();
        System.out.println(str1);
        System.out.println("Animal Object instantiation");
    }


}

class Dog extends Animal{

    static String str = "I`m a dog!";

    private String str1 = "I`m 10 years old @Dog";

    static {
        System.out.println(str);
        System.out.println("Load subclass static code block");
    }

    static void doing() {
        System.out.println("This ia a dog!");
    }

    public Dog() {
        doing();
        System.out.println(str1);
        System.out.println("Dog Object instantiation");
    }


    public static void main(String[] args) {
        new Dog();
    }
}

Operation result:

I`m a animal!

Load parent static code block

I`m a dog!

Load subclass static code block

This ia a Animal!

I`m 10 years old @Animal

Animation object instantiation

This ia a dog!

I`m 10 years old @Dog

Dog object instantiation

Let's take a look at the overridden parent method when loading the parent.

Example:

package com.xm.load;

public class Animal {

    static String str = "I`m a animal!";

    public String str1 = "I`m 10 years old @Animal";

    static {
        System.out.println(str);
        System.out.println("Load parent static code block");
    }

    static void doing() {
        System.out.println("This ia a Animal!");
    }

    public void todoing() {
        System.out.println(str1);
        System.out.println("Override method of loading subclass");
    }

    public Animal() {
        doing();
        todoing();
        System.out.println("Animal Object instantiation");
    }


}

class Dog extends Animal{

    static String str = "I`m a dog!";

    private String str1 = "I`m 10 years old @Dog";

    static {
        System.out.println(str);
        System.out.println("Load subclass static code block");
    }

    static void doing() {
        System.out.println("This ia a dog!");
    }

    public void todoing() {
        System.out.println(str1);
        System.out.println("Override method of loading subclass");
    }

    public Dog() {
        doing();
        System.out.println("Dog Object instantiation");
    }


    public static void main(String[] args) {
        new Dog();
    }
}

Result:

I`m a animal!

Load parent static code block

I`m a dog!

Load subclass static code block

This ia a Animal!

null

Override method of loading subclass

Animation object instantiation

This ia a dog!

Dog object instantiation

It can be seen that null means that when loading the parent constructor, the todoing() method called is the method of the subclass, and the initialization of the properties of the subclass occurs after the parent constructor.

Tags: Java

Posted on Tue, 03 Dec 2019 19:49:47 -0800 by onno182