首页 > 学院 > 开发设计 > 正文

关于Java中类加载时对其属性和代码块初始化顺序的研究与测试

2019-11-17 04:15:16
字体:
来源:转载
供稿:网友

  关于类加载时对其内部属性,代码块初始化(通常是分配栈内存)的顺序的测试结果如下几点:
 * 1,类的加载,这是由类加载器执行的,该步骤将查找字节码(通常是在classPath的类路径下)但这种并非是必须的,并从这些字节码中创建Class对象。
 * 2,链接,在链接阶段将验证类中的字节码,为静态域分配存储空间,并且如果必须的话。将解析这个类创建的对其他类的所有引用。所有的带有static关键字的成员变量和静态代码块的存储空间就是在这个时候分配的(其中他们的分配空间的顺序是按照代码的先后顺序的)。
 * 3,初始化,如果该类具有超类,则对其初始化,执行静态初始化器和静态初始化块(对成员变量和静态初始化块初始化)。
 * 首先明确一点,就是不管类的构造器是否对其成员变量进行手动赋值,类在加载的时候都会为其成员变量
 * 初始化的,并且它的初始化时机要比构造器初始化早的多(构造器初始化是发生在new对象的时候),
 * 对成员变量和静态初始化块初始化得顺序按照当前的代码先后顺序。

以下为测试代码:

测试结果:

+ expand sourceview plaincopy to clipboardPRint?
public class StaticInitialization {  
 
    public static void main(String[] args) {  
        System.out.println("Creating new Cupboard() in main");  
        new Cupboard();  
        System.out.println("Creating new Cupboard() in main");  
        new Cupboard();  
        table.f2(1);  
        cupboard.f3(1);  
    }  
    static Table table = new Table();  
    static Cupboard cupboard = new Cupboard();  
}  
class Bowl {  
    static int i = 9;  
      
    {  
        //静态初始化块  
        System.out.println(i);  
    }  
      
    static {  
        //静态代码块  
        System.out.println("static:" + i);  
    }  
    Bowl(int marker) {  
        System.out.println("Bowl(" + marker + ");");  
    }  
    void f1(int marker) {  
        System.out.println("f1(" + marker + ");");  
    }  
}  
class Table {  
    static Bowl bowl1 = new Bowl(1);  
    Table() {  
        System.out.println("Table();");  
        bowl2.f1(1);  
    }  
    void f2(int marker) {  
        System.out.println("f2(" + marker + ");");  
    }  
    static Bowl bowl2 = new Bowl(2);  
}  
class Cupboard {  
    Bowl bowl3 = new Bowl(3);  
    static Bowl bowl4 = new Bowl(4);  
    Cupboard() {  
        System.out.println("Cupboard();");  
        bowl4.f1(2);  
    }  
    void f3(int marker) {  
        System.out.println("f3(" + marker + ");");  
    }  
    static Bowl bowl5 = new Bowl(5);  

public class StaticInitialization {

    public static void main(String[] args) {
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        table.f2(1);
        cupboard.f3(1);
    }
    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
}
class Bowl {
    static int i = 9;
   
    {
        //静态初始化块
        System.out.println(i);
    }
   
    static {
        //静态代码块
        System.out.println("static:" + i);
    }
    Bowl(int marker) {
        System.out.println("Bowl(" + marker + ");");
    }
    void f1(int marker) {
        System.out.println("f1(" + marker + ");");
    }
}
class Table {
    static Bowl bowl1 = new Bowl(1);
    Table() {
        System.out.println("Table();");
        bowl2.f1(1);
    }
    void f2(int marker) {
        System.out.println("f2(" + marker + ");");
    }
    static Bowl bowl2 = new Bowl(2);
}
class Cupboard {
    Bowl bowl3 = new Bowl(3);
    static Bowl bowl4 = new Bowl(4);
    Cupboard() {
        System.out.println("Cupboard();");
        bowl4.f1(2);
    }
    void f3(int marker) {
        System.out.println("f3(" + marker + ");");
    }
    static Bowl bowl5 = new Bowl(5);
}   


输出结果:

 

9
Bowl(1);
9
Bowl(2);
Table();
f1(1);
9
Bowl(4);
9
Bowl(5);
9
Bowl(3);
Cupboard();
f1(2);
Creating new Cupboard() in main
9
Bowl(3);
Cupboard();
f1(2);
Creating new Cupboard() in main
9
Bowl(3);
Cupboard();
f1(2);
f2(1);
f3(1);


具体java虚拟机是怎么管理内存的,和它对堆和栈是如何处理的,还需请教高手指点!
-------此文章仅为本人Java的复习总结


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表