这个执行顺序与类的加载有关,先看代码:
package com.gs;
class Fruit{
private String colour;
static {
System.out.println(" 1 Fruit static");
}
{
System.out.println(" 1 Fruit common");
}
public Fruit() {
System.out.println(" 1 Fruit construction");
}
}
class Apple extends Fruit{
static {
System.out.println(" 2 Apple static");
}
{
System.out.println(" 2 Apple common");
}
public Apple() {
System.out.println(" 2 Apple construction");
}
}
public class ExecuteOrder {
static {
System.out.println(" 3 ExecuteOrder static");
}
{
System.out.println(" 3 ExecuteOrder common");
}
public ExecuteOrder() {
System.out.println(" 3 ExecuteOrder construction");
}
static public void main(String[] args) {
System.out.println("#######################");
Apple apple1 = new Apple();
System.out.println("----------");
Apple apple2 = new Apple();
}
}
执行的结果,如下:
3 ExecuteOrder static
#######################
1 Fruit static
2 Apple static
1 Fruit common
1 Fruit construction
2 Apple common
2 Apple construction
----------
1 Fruit common
1 Fruit construction
2 Apple common
2 Apple construction
加载ExecuteOrder.class的过程,(1)加载ExecuteOrder.class文件,(2)如果这个class文件,有依赖别的class文件,则加载别的class文件,(3)执行这个ExecuteOrder.class文件中的main函数(main函数是静态函数)。
由以上结果可知,静态代码块是在类加载的阶段执行的。
-------------------------------------------------------------------------------------------------------------------------------------
修改main函数,如下:
class Fruit{
private String colour;
public static int XXX = 11;
static {
System.out.println(" 1 Fruit static");
}
{
System.out.println(" 1 Fruit common");
}
public Fruit() {
System.out.println(" 1 Fruit construction");
}
public static int getXXX() {
return XXX;
}
}
public class ExecuteOrder {
static {
System.out.println(" 3 ExecuteOrder static");
}
{
System.out.println(" 3 ExecuteOrder common");
}
public ExecuteOrder() {
System.out.println(" 3 ExecuteOrder construction");
}
static public void main(String[] args) {
System.out.println("#######################");
System.out.println("XXX=" + Fruit.XXX);
System.out.println("XXX=" + Fruit.getXXX());
}
}
(1)当静态变量 public static int XXX = 11; ,没有 final 修饰时,打印如下:
3 ExecuteOrder static
#######################
1 Fruit static
XXX=11
XXX=11
(2)当静态变量 public static final int XXX = 11; ,有 final 修饰时,打印如下:
3 ExecuteOrder static
#######################
XXX=11
1 Fruit static
XXX=11
原因分析:类的生命周期分为:加载、连接(验证、准备、解析)、初始化、使用、卸载。java虚拟机规范规定,初始化的情况,其中有一种是,被final修饰的static变量,是在编译器把结果放入常量池中。参见《深入理解java虚拟机》7.2节。