未完Java class&interfac 's Loading, Linking and Initializing | 类与接口的加载、链接和初始化

JVM装载一个类的时候,首先检查他有没有父类,如果有父类则装载父类,然后再装载该类,装载!=实例化,但要开辟内存,这些类都放在JVM的方法区内,类实例化后的对象放在JVM的堆内。
实例化一个子类时,父类一定被装载,但并不是实例化。?????



Bill Venners的书中对Load/link/init的讲解,非常棒:
《Inside the Java Virtual Machine》Chapter 7 - The Lifetime of a Type:
http://www.artima.com/insidejvm/ed2/lifetype.html
《Objects and Java》Chapter 4 - Initialization and Cleanup(前段讲对象初始化,后段讲的才是类初始化):
http://www.artima.com/objectsandjava/webuscript/InitCleanup1.html



一些名词概念:
Class Initialization Method : <clinit>
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.9

Class Variable Initializers:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2
给个例子就明白了:
class CoffeeCup {
    private static int innerCoffee = 355; // "= 355" is an Class Variable Initializers
    // ...
}

Static Initializers:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.7
即静态初始化块(Static Initialization Blocks)。
静态初始化块在类初始化的时候被执行。
存在多个静态初始化块时,按照他们在代码中出现的顺序依次执行。
静态初始化块中不能有return语句。
静态初始化块中不能使用this和super,不能访问类的实例变量。
非静态初始化块(Non-static Initialization Blocks)参见:
http://wuaner.iteye.com/blog/1669937

Sources:

Java Virtual Machine Specification - Chapter 5. Loading, Linking, and Initializing:
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html
JLS 12.4 Initialization of Classes and Interfaces:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4




请不要将类的初始化(Initialization of Classes)等同于类的实例化(对象初始化)。类的实例化(对象初始化)参见:
http://wuaner.iteye.com/admin/blogs/1669937




Static Initializers & Class Variable Initializers示例:
public class StaticVariableTest {  
    private static StaticVariableTest svt = new StaticVariableTest();//语句(1)  
    private static int count1;//语句(2)  
    private static int count2 = 0;//语句(3)  
      
    private StaticVariableTest(){//语句(4)  
        count1++;  
        count2++;  
    }  
    public static StaticVariableTest getInstance(){//语句(5)  
        return svt;  
    }  
    public static int getCount1() {  
        return count1;  
    }  
    public static void setCount1(int count1) {  
        StaticVariableTest.count1 = count1;  
    }  
    public static int getCount2() {  
        return count2;  
    }  
    public static void setCount2(int count2) {  
        StaticVariableTest.count2 = count2;  
    }  
    public static void main(String[] args) {  
        StaticVariableTest svt = StaticVariableTest.getInstance();//语句(6)  
        System.out.println("count1:" + svt.getCount1());//语句(7)  输出 1
        System.out.println("count2:" + svt.getCount2());//语句(8)  输出 0
    }  
}  

你可能感兴趣的:(java,类初始化,类加载,loading,Initializing)