初始化及类的加载


类中静态代码、非静态代码、构造函数等在初始化时它们的顺序是不一样的。


1. 静态代码的初始化

静态代码包括static field和static block


例1:如下代码:

class Bowl {                                                                                                                 
    Bowl(int marker) {          //(1-1-1) (1-2-1)(3-1-1)(3-2-1)(3-3-1)
        System.out.println("Bowl(" + marker + ")");  
    }

    void f1(int marker) {                           //(1-3-1)(3-4-1)
        System.out.println("f1(" + marker + ")");
    }
}

class Table {
    static Bowl bowl1 = new Bowl(1);                       //(1-1)
    Table() {                                              //(1-3)
        System.out.println("Table()");  
        bowl2.f1(1);                             
     }

    void f2(int marker) {                               
        System.out.println("f2(" + marker + ")");
    }
    static Bowl bowl2 = new Bowl(2);                      //(1-2)
}

class Cupboard {
    Bowl bowl3 = new Bowl(3);                            //(3-3)
    static Bowl bowl4 = new Bowl(4);                     //(3-1)
    Cupboard() {                                         //(3-4)
        System.out.println("Cupboard()");
        bowl4.f1(2);
    }
    void f3(int marker) { 
        System.out.println("f3(" + marker + ")");
    }
    static Bowl bowl5 = new Bowl(5);                    //(3-2)
}

public class StaticInitialization {
    public static void main(String[] args) {
        System.out.println("Creating new Cupboard() in main");  //(4)
        new Cupboard();                                         //(5)
        System.out.println("Creating new Cupboard() in main");  //(6)
        new Cupboard();                                         //(7)
        table.f2(1);                                            //(8)
        cupboard.f3(1);                                         //(9)
    }
    static Table table = new Table();                      //(1)
    static {                                               //(2)
        System.out.println("between table and cupboart");
    }
    static Cupboard cupboard = new Cupboard();             //(3)
} 

静态代码的初始化以及非静态实例的初始化在这个例子中都包括了。

结果如下:



2. 带继承的初始化

例2,如下代码:

class Insect {
    private int i = 9;
    protected int j;
    Insect() {
        System.out.println("i = " + i + ", j = " + j);
        j = 39;
    }
    private static int x1 = printInit("static Insect.x1 initialized");
    static int printInit(String s) {
        System.out.println(s);
        return 47;
    }
}

public class Beetle extends Insect {
    private int k = printInit("Beetle.k initialized");
    public Beetle() {
        System.out.println("k = " + k);
        System.out.println("j = " + j);
    }
    private static int x2 = printInit("static Beetle.x2 initialized");                                                       
    public static void main(String[] args) {
        System.out.println("Beetle constructor");
        Beetle b = new Beetle();
  }
}

输出:



3. 初始化顺序总结


初始化按照下面的顺序执行:

(1) 静态代码的初始化:包括static field 和 static block。静态代码的初始化在当加载类的时候执行,即首次用到类的时候,如首次访问类的静态成员、静态函数,创建该类的对象。

注意:静态代码的初始化在加载类时执行且仅执行一次!


(2) 实例代码初始化:包括实例变量和实例代码块。创建对象时进行实例变量的初始化。更确切的说是在构造函数显式或隐式的调用父构造函数之后(即父构造函数执行完成之后,是个递归的过程)完成初始化。例2充分说明了这一点。


(3) 构造函数执行:在完成实例代码初始化后,本类的构造函数开始执行,此时构造函数中显式或隐式的调用父构造函数已经执行完毕,在(2)实例代码的初始化之前就已经完成。


4. 原始类型和对象类型的默认初始化值

(1) 实例变量和静态变量



(2) 局部变量

局部变量没有默认值,在使用直线必须初始化,否则出现编译器错误。


(3) 已分配空间的数组的默认值

对于已分配空间的数组中的每个值来说,无论此数组是实例数组还是局部数组都会有默认值,根据类型的不同,默认值与(1)中表格中的一样。

如下代码:

public class ArrayInit {
    public void test() {
        int c = 1;
        int b;    //compiler error, it must be initialized before used                    
        int[] a = new int[2];      // primitice types, default values are 0
        String[] s = new String[2];// reference types, default values are null
        System.out.println(c + " " + b + " " + a[0] + " " + s[0]);
    }
    public static void main(String[] args) {
        new ArrayInit().test();
    }
}

编译结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值