下面代码显示了静态初始化代码块,非静态实例初始化代码块,构造函数 等在Java中从代码载入内存到代码执行过程的执行顺序:
import static net.mindview.util.Print.*;
public class inheritancetest {
public static void main(String[] args) {
Cartoon ct = new Cartoon();
}
}
class Art {
{
print("Art instance block.");
}
static {
print("Art static block.");
}
public Art() {
System.out.println("Art constructor!!!");
}
}
class Drawing extends Art {
{
print("Drawing instance block.");
}
static {
print("Drawing static block");
}
public Drawing() {
System.out.println("Drawing constructor!!!");
}
}
class Cartoon extends Drawing {
{
print("cartoon instance block");
}
static {
print("cartoon static block.");
}
public Cartoon() {
print("Cartoon constructor!!!");
}
}
输出结果为:
Art static block.
Drawing static block
cartoon static block.
Art instance block.
Art constructor!!!
Drawing instance block.
Drawing constructor!!!
cartoon instance block
Cartoon constructor!!!
父类显式静态初始化代码块 > 子类显式静态初始化代码块 > 父类非静态实例初始化代码块 > 父类构造函数 > 子类非静态实例初始化代码块 > 子类构造函数
final关键字对变量初始化的影响,如下程序:
import static net.mindview.util.Print.*;
import java.util.Random;
public class FinalData {
private static Random rand = new Random(47);
private String id;
public FinalData(String id) {
this.id = id; blankfinal = id;
}
private final int valueOne = 9;
private static final int VALUE_TWO = 99;
public static final int VALUE_THREE = 39;
private final int i4 = rand.nextInt(20);
static final int INT_5 = rand.nextInt(20);
private Value v1 = new Value();
private final Value v2 = new Value();
private static final Value VAL_3 = new Value();
private final int[] a = {1,2,3,4,5,6};
private final String blankfinal;
public String toString() {
return id + ": " + "final i4 = " + i4 +", static final INT_5 = " + INT_5 + ", v1:" + v1.toString() + ", final v2:" + v2.toString() + ", static final VAL_3:" + VAL_3.toString() + ", final a = " + a;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FinalData fd1 = new FinalData("fd1");
fd1.v2.i++;
fd1.v1 = new Value();
for(int i=0; i<fd1.a.length; i++) {
fd1.a[i]++;
}
print(fd1);
print("Creating new FinalData");
FinalData fd2 = new FinalData("fd2");
print(fd1);
print(fd2);
}
}
class Value {
/**
* Package access
*/
int i;
public static int con = 0;
public Value() {
this.i = con;
con++;
}
public String toString() {
return "(Value)i=" + i;
}
}
输出为:
fd1: final i4 = 15, static final INT_5 = 18, v1:(Value)i=3, final v2:(Value)i=3, static final VAL_3:(Value)i=0, final a = [I@4926097b
Creating new FinalData
fd1: final i4 = 15, static final INT_5 = 18, v1:(Value)i=3, final v2:(Value)i=3, static final VAL_3:(Value)i=0, final a = [I@4926097b
fd2: final i4 = 13, static final INT_5 = 18, v1:(Value)i=4, final v2:(Value)i=5, static final VAL_3:(Value)i=0, final a = [I@762efe5d
Summary:
1. 静态初始化优先级最高。
2. 非静态实例与构造函数处于相同等级,但是非静态实例初始化先于构造函数执行。
3. static final的变量属于package,仅初始化一次。
4. final修饰的引用变量每次生成实例的时候都会初始化,并且之后变量的引用不可更改,但是变量引用的对象内部的内容可以更改,Java也并没有机制来保持对象的恒定不变。
5. 上面的int数组a,是两个内部值相同的数组,两个实体对象。
6. blank final(空白final)变量引用必须在创建的时候(构造函数中)进行初始化。