A clear example of Java class load order

When we have a new GirlFriend, we have done what?

One example they get to know Java program running order

public class Girl {
    Person person = new Person("Girl");

    static{
        System.out.println("Girl static");
    }

    static Person staticPerson = new Person("GirlStaticPerson");

    public Girl() {
        System.out.println("Girl constructor");
    }

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

class Person{
    static{
        System.out.println("person static");
    }

    static Person staticPerson = new Person("PersonStaticPerson");

    public Person(String str) {
        System.out.println("person "+str);
    }
}


class MyGirlFriend extends Girl {
    Person person = new Person("MyGirlFriend");

    static Person myStaticPerson = new Person("MyStaticPerson");

    static{
        System.out.println("MyGirlFriend static");
    }

    public MyGirlFriend() {
        System.out.println("MyGirlFriend constructor");
    }
}

This code includes both inherited and static, here I have to carefully analyze what the results of running this code will be.
First, the implementation of java Girlafter, jvm will look Girl.class, after finding loads, load time will be executed first static code block and static member variables (execution order) , and therefore executed first static{ System.out.println("Girl static"); }
and then initialize static member variables, perform this sentence: static Person staticPerson = new Person("GirlStaticPerson");, then look for the Person class and load, therefore begin Person of static code blocks and static member variables, namely the implementation of this sentence: static{ System.out.println("person static"); }followed by a static member variables: static Person staticPerson = new Person("PersonStaticPerson");time has loaded the Person class, which began execution public Person(String str) { System.out.println("person "+str); }method
then back to class Girl , person class has been loaded successfully, the constructor execution person and print person GirlStaticPerson
at this time Girl class static area has been performed, the main method begins execution, that is the main program: new MyGirlFriend();then look MyGirlFriend class and loads, performing a static member variables after loading: static Person myStaticPerson = new Person("MyStaticPerson");execute person constructor print person MyStaticPerson, then the static code block: static{ System.out.println("MyGirlFriend static"); }
At this point MyGirlFriend already loaded, ready to perform construction method, and then found MyGirlFriend inherited Girl, you need to run the parent class constructor, before the implementation of the parent class constructor and the need to initialize the parent class member variables , class execution Girl i.e. Person person = new Person("Girl");the call line P erson constructor, print person Girl, and then will run a parent constructor: public Girl() { System.out.println("Girl constructor"); }
After the completion of the implementation of the parent class constructor MyGirlFriend back to class, ready to perform subclass constructors, its members need to initialize variables before executing the construction method, which is executed : Person person = new Person("MyGirlFriend");Finally, is the subclass constructor:public MyGirlFriend() { System.out.println("MyGirlFriend constructor"); }
At this point, back to the main method, that line of code is finished. Operating results are:

Girl static//Girl类静态代码块
person static//Person类静态代码块
person PersonStaticPerson//Person类中的静态Person成员变量
person GirlStaticPerson//Girl类中的静态Person成员变量
person MyStaticPerson//MyGirlFriend类中的静态成员变量
MyGirlFriend static//MyGirlFriend类中的静态代码块
person Girl//Girl类的成员变量
Girl constructor//Girl类的构造方法
person MyGirlFriend//MyGirlFriend类的成员变量
MyGirlFriend constructor//MyGirlFriend构造方法

However, when the breakpoint is hit at IDEA in this line:
Breakpoints
occasional strange operating results the following:
strange
personal guess is that the idea of debug mode in advance to load the class, so that the static member variables and static code block runs ahead

to sum up: No girlfriend a new way o ((> ω <)) o
After the first non-stationary, the sub Fathers, after the first block is


Reference to this blog

Guess you like

Origin www.cnblogs.com/lixin-link/p/11069656.html