java类的初始化顺序

来源:互联网 发布:网络歌手安小沫 编辑:程序博客网 时间:2024/06/05 07:46

面试的时候,会提到java类的初始化顺序,下面以我的理解来说一下这个知识点


加载类:主要是初始化静态数据:如静态变量,静态块(final类型的静态变量存在常量池中,不会被加载,

有时候会有例外) 

构造类:先初始化类成员变量(非静态),之后调用构造器


new出一个对象,先加载类,之后构造类

加载类和构造类,是不一样的,特别注意,加载类的时候不一定要实例化对象,之后的例子会说明


以下几种情况会加载类

1:初学者最为常用new出一个了类的实例对象

2:对类的静态变量进行读取,赋值操作(静态,final且值确定是常量,是编译时确定的,调用时,不会加载类)

3:直接调用类的静态方法

4:反射调用一个了类的方法

构造类的话就是你实例了一个对象的情况


代码1:不构造类只是加载类的情况

class  A{         public static int  staticData= 4 ;         static     {             System.out.println("类被加载");         }           A()    {    System.out.println("类被构造");    }}         public class TestOne{         public static void main(String[] args)    {             System.out.println(A.staticData);         }         }    /*结果:类被加载4*/


代码2:对加载类时,对final类型变量的讨论

<span style="font-size:18px;">class  A{         public final static int  staticData= 4 ;         static     {             System.out.println("类被加载");         }           A()    {    System.out.println("类被构造");    }}         public class TestOne{         public static void main(String[] args)    {             System.out.println(A.staticData);         }         }    /*结果:4*/</span>
<span style="font-size:18px;">class  A{         public final static int  staticData= 4 + new Random().nextInt(10) ;         static     {             System.out.println("类被加载");         }           A()    {    System.out.println("类被构造");    }}         public class TestOne{         public static void main(String[] args)    {             System.out.println(A.staticData);         }         }    /*结果:类被加载5*/</span>
初看有点难以理解,单细细想,就能理解了,首先第一个程序,final在编译的时候已经确定了其值,放在常量

池中,所以main函数访问其,直接去常量池,不会去加载类,而第二个程序编译的时候无法确定其值,所以必须

加载其对应的类才能访问到其数据!


代码三:对加载类,构造类的全面讨论

引用了think in java中的一段代码:

class Bowl {    Bowl(int marker)     {        System.out.println("Bowl(" + marker + ")");    }    void f1(int marker)     {        System.out.println("f1(" + marker + ")");    }}class Table {    Bowl bowl1 = new Bowl(1);    Table()     {        System.out.println("Table()");        bowl2.f1(2);    }    void f2(int marker)     {        System.out.println("f2(" + marker + ")");    }    static Bowl bowl6;    static Bowl bowl2 = new Bowl(2);    static void fs(int marker)     {        System.out.println("fs(" + marker + ")");    }    static     {        bowl6 = new Bowl(6);    }    static Bowl bowl7 = new Bowl(7);}class Cupboard {    Bowl bowl3 = new Bowl(3);    static Bowl bowl4 = new Bowl(4);    public Cupboard()     {        System.out.println("Cupboard()");        bowl4.f1(4);    }    void f3(int marker)     {        System.out.println("f3(" + marker + ")");    }    static Bowl bowl5 = new Bowl(5);}public class  ClassInitialization {    public static void main(String[] args)    {        System.out.println("now in main");        Table.fs(0);        System.out.println("Creating new Cupboard in main");        new Cupboard();        System.out.println("Creating new Table in main");        Table table = new Table();        table.f2(1);        cupboard.f3(1);    }    static Cupboard cupboard = new Cupboard();  }/* 结果: Bowl(4) Bowl(5) Bowl(3) Cupboard() f1(4) now in main Bowl(2) Bowl(6) Bowl(7) fs(0) Creating new Cupboard in main Bowl(3) Cupboard() fi(4) Creating new Table in main Bowl(1) Table f1(2) f2(1) f3(1) */

建议先写出自己的结果,然后在与正确答案对一下,这样对类初始化的顺序更加印象深刻

如果你看了我上面写的东西,做对这道题并不难,还是要多学习


0 0
原创粉丝点击