1.直接定义本类,如:A a = new A();此时就直接调用A 的构造方法public A(){}。
2.定义所在类的子类,如class B extends A{}。。。。。A a = new B();此时先调用静态方法(先父类的静态,再子类的静态),再调用构造方法(先父类,再子类)。
下面赋代码实例:
public class Lei_test {
public static void main(String[] args) {
A a = new B(); //输出ABab
System.out.println("-------------------------");
a = new A(); //输出a
System.out.println("-------------------------");
a = new B(); //输出ab
System.out.println("-------------------------");
A1 a1 = new A(); //输出a
System.out.println("-------------------------");
B b = new A(); //报错类型不符合
System.out.println("-------------------------");
}
}
class A1 {
static {
System.out.println("A");
}
public A1() {
System.out.println("a");
}
}
class B extends A1 {
static {
System.out.println("B");
}
public B() {
System.out.println("b");
}
}
好了以上就是类的加载顺序了,这个在面试的时候会经常考,如果还有什么要补充的可以在评论区留言。