關(guān)于類加載時對其內(nèi)部屬性,代碼塊初始化(通常是分配棧內(nèi)存)的順序的測試結(jié)果如下幾點:
* 1,類的加載,這是由類加載器執(zhí)行的,該步驟將查找字節(jié)碼(通常是在classPath的類路徑下)但這種并非是必須的,并從這些字節(jié)碼中創(chuàng)建Class對象。
* 2,鏈接,在鏈接階段將驗證類中的字節(jié)碼,為靜態(tài)域分配存儲空間,并且如果必須的話。將解析這個類創(chuàng)建的對其他類的所有引用。所有的帶有static關(guān)鍵字的成員變量和靜態(tài)代碼塊的存儲空間就是在這個時候分配的(其中他們的分配空間的順序是按照代碼的先后順序的)。
* 3,初始化,如果該類具有超類,則對其初始化,執(zhí)行靜態(tài)初始化器和靜態(tài)初始化塊(對成員變量和靜態(tài)初始化塊初始化)。
* 首先明確一點,就是不管類的構(gòu)造器是否對其成員變量進行手動賦值,類在加載的時候都會為其成員變量
* 初始化的,并且它的初始化時機要比構(gòu)造器初始化早的多(構(gòu)造器初始化是發(fā)生在new對象的時候),
* 對成員變量和靜態(tài)初始化塊初始化得順序按照當(dāng)前的代碼先后順序。
以下為測試代碼:
測試結(jié)果:
+ 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;
{
//靜態(tài)初始化塊
System.out.println(i);
}
static {
//靜態(tài)代碼塊
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;
{
//靜態(tài)初始化塊
System.out.println(i);
}
static {
//靜態(tài)代碼塊
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);
}
輸出結(jié)果:
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虛擬機是怎么管理內(nèi)存的,和它對堆和棧是如何處理的,還需請教高手指點!
-------此文章僅為本人Java的復(fù)習(xí)總結(jié)
新聞熱點
疑難解答