<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1、自下而上加载类的编译代码</span>
运行java程序时,首先要找main(),于是启动加载器加载main对应类的编译代码,在加载过程中,若发现有基类,则加载基类,以此类推。也就是从基类向下加载
2、自上而下的初始化static
可以看作是类加载时进行的,static初始化顺序,就是类加载顺序
3、默认初始化:基本类型和对象类型
4、基类构造器
5、变量定义的顺序显式初始化
6、自身的构造器
7、例:
class Meal {
Meal() { System.out.println("Meal()"); }
static {
System.out.println("Meal static");
}
}
class Bread {
Bread() { System.out.println("Bread()"); }
}
class Cheese {
Cheese() { System.out.println("Cheese()"); }
static {
System.out.println("Cheese static");
}
}
class Lettuce {
Lettuce() { System.out.println("Lettuce()"); }
}
class Lunch extends Meal {
Lunch() { System.out.println("Lunch()"); }
static {
System.out.println("Lunch static");
}
}
class PortableLunch extends Lunch {
PortableLunch() { System.out.println("PortableLunch()"); }
static {
System.out.println("PortableLunch static");
}
}
public class Sandwich extends PortableLunch {
private Bread b = new Bread();
private Cheese c = new Cheese();
public Sandwich() { System.out.println("Sandwich()"); }
private Lettuce l = new Lettuce();
static {
System.out.println("Sandwich static");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Sandwich();
}
}
