jdk1.5輸入:
Exception in thread "main" Java.lang.ArithmeticException: / by zero
at debug.Test.
at debug.Test.main(Test.Java:7)
JVM並沒有把異常定位在Test()構造方法中,而是在debug.Test.
當我們看到了這兩個方法以後,我們再來詳細討論這兩個“內置初始化方法”(我並不喜歡生造一些非標准的術語,但我確實不知道如何規范地稱呼他們)。
內置初始化方法是JVM在內部專門用於初始化的特有方法,而不是提供給程序員調用的方法,事實上“<>”這樣的語法在源程序中你連編譯都無法通過。這就說明,初始化是由JVM控制而不是讓程序員來控制的。
類初始化方法:
我沒有從任何地方了解到
在類裝載(load)時,JVM會調用內置的
我們稍微增加兩行static語句:
package debug;
public class Test {
static int x = 0;
static String s = "123";
static {
String s1 = "456";
if(1==1)
throw new RuntimeException();
}
public static void main(String[] args) {
new Test();
}
}
然後進行反編譯:
Javap -c debug.Test
Compiled from "Test.Java"
public class debug.Test extends Java.lang.Object{
static int x;
static Java.lang.String s;
public debug.Test();
Code:
0: aload_0
1: invokespecial #1; //Method Java/lang/Object."":()V
4: return
public static void main(Java.lang.String[]);
Code:
0: new #2; //class debug/Test
3: dup
4: invokespecial #3; //Method "":()V
7: pop
8: return
static {};
Code:
0: iconst_0
1: putstatic #4; //FIEld x:I
4: ldc #5; //String 123
6: putstatic #6; //FIEld s:LJava/lang/String;
9: ldc #7; //String 456
11: astore_0
12: new #8; //class Java/lang/RuntimeException
15: dup
16: invokespecial #9; //Method Java/lang/RuntimeException."":()V
19: athrow
}
我們可以看到,類初始化正是按照源文件中定義的原文順序進行。先是聲明
static int x;
static Java.lang.String s;
然後對int x和String s進行賦值:
0: iconst_0
1: putstatic #4; //FIEld x:I
4: ldc #5; //String 123
6: putstatic #6; //FIEld s:LJava/lang/String;
執行初始化塊的String s1 = "456";生成一個RuntimeException拋
9: ldc #7; //String 456
11: astore_0
12: new #8; //class Java/lang/RuntimeException
15: dup
16: invokespecial #9; //Method Java/lang/RuntimeException."":()V
19: athrow
要明白的是,"
的屬性都是內聯的,只有直接賦常量值的接口常量才會內聯。而
[public static final] double d = Math.random()*100;
這樣的表達式是需要計算的,在接口中就要由"
下面我們再來看看實例初始化方法"
"
package debug;
public class Test {
int x = 0;
String s = "123";
{
String s1 = "456";
//if(1==1)
//throw new RuntimeException();
}
public Test(){
String ss = "789";
}
public static void main(String[] args) {
new Test();
}
}
Javap -c debug.Test的結果:
Compiled from "Test.Java"
public class debug.Test extends Java.lang.Object{
int x;
Java.lang.String s;
public debug.Test();
Code:
0: aload_0
1: invokespecial #1; //Method Java/lang/Object."":()V
4: aload_0
5: iconst_0
6: putfield #2; //FIEld x:I
9: aload_0
10: ldc #3; //String 123
12: putfield #4; //FIEld s:LJava/lang/String;
15: ldc #5; //String 456
17: astore_1
18: ldc #6; //String 789
20: astore_1
21: return
public static void main(Java.lang.String[]);
Code:
0: new #7; //class debug/Test
3: dup
4: invokespecial #8; //Method "":()V
7: pop
8: return
}
如果在同一個類中,一個構造方法調用了另一個構造方法,那麼對應的"
package debug;
public class Test {
String s1 = rt("s1");
String s2 = "s2";
public Test(){
s1 = "s1";
}
public Test(String s){
this();
if(1==1) throw new Runtime();
}
String rt(String s){
return s;
}
public static void main(String[] args) {
new Test("");
}
}
反編譯的結果:
Compiled from "Test.Java"
public class debug.Test extends Java.lang.Object{
Java.lang.String s1;
Java.lang.String s2;
public debug.Test();
Code:
0: aload_0
1: invokespecial #1; //Method Java/lang/Object."":()V
4: aload_0
5: aload_0
6: ldc #2; //String s1
8: invokevirtual #3; //Method rt:(Ljava/lang/String;)LJava/lang/String;
11: putfield #4; //FIEld s1:LJava/lang/String;
14: aload_0
15: ldc #5; //String s2
17: putfield #6; //FIEld s2:LJava/lang/String;
20: aload_0
21: ldc #2; //String s1
23: putfield #4; //FIEld s1:LJava/lang/String;
26: return
public debug.Test(Java.lang.String);
Code:
0: aload_0
1: invokespecial #7; //Method "":()V
4: new #8; //class Java/lang/RuntimeException
7: dup
8: invokespecial #9; //Method Java/lang/RuntimeException."":()V
11: athrow
java.lang.String rt(Java.lang.String);
Code:
0: aload_1
1: areturn
public static void main(Java.lang.String[]);
Code:
0: new #10; //class debug/Test
3: dup
4: ldc #11; //String
6: invokespecial #12; //Method "":(LJava/lang/String;)V
9: pop
10: return
}
我們看到,由於Test(String s)調用了Test();所以"
public debug.Test(Java.lang.String);
Code:
0: aload_0
1: invokespecial #7; //Method "":()V
4: new #8; //class Java/lang/RuntimeException
7: dup
8: invokespecial #9; //Method Java/lang/RuntimeException."":()V
11: athrow
而如果兩個構造方法是相互獨立的,則每個構造方法調用前都會執行實例變量和初始化塊的調用:
package debug;
public class Test {
String s1 = rt("s1");
String s2 = "s2";
{
String s3 = "s3";
}
public Test() {
s1 = "s1";
}
public Test(String s) {
if (1 == 1)
throw new RuntimeException();
}
String rt(String s) {
return s;
}
public static void main(String[] args) {
new Test("");
}
}
反編譯的結果:
Compiled from "Test.Java"
public class debug.Test extends Java.lang.Object{
Java.lang.String s1;
Java.lang.String s2;
public debug.Test();
Code:
0: aload_0
1: invokespecial #1; //Method Java/lang/Object."":()V
4: aload_0
5: aload_0
6: ldc #2; //String s1
8: invokevirtual #3; //Method rt:(Ljava/lang/String;)LJava/lang/String;
11: putfield #4; //FIEld s1:LJava/lang/String;
14: aload_0
15: ldc #5; //String s2
17: putfield #6; //FIEld s2:LJava/lang/String;
20: ldc #7; //String s3
22: astore_1
23: aload_0
24: ldc #2; //String s1
26: putfield #4; //FIEld s1:LJava/lang/String;
29: return
public debug.Test(Java.lang.String);
Code:
0: aload_0
1: invokespecial #1; //Method Java/lang/Object."":()V
4: aload_0
5: aload_0
6: ldc #2; //String s1
8: invokevirtual #3; //Method rt:(Ljava/lang/String;)LJava/lang/String;
11: putfield #4; //FIEld s1:LJava/lang/String;
14: aload_0
15: ldc #5; //String s2
17: putfield #6; //FIEld s2:LJava/lang/String;
20: ldc #7; //String s3
22: astore_2
23: new #8; //class Java/lang/RuntimeException
26: dup
27: invokespecial #9; //Method Java/lang/RuntimeException."":()V
30: athrow
java.lang.String rt(Java.lang.String);
Code:
0: aload_1
1: areturn
public static void main(Java.lang.String[]);
Code:
0: new #10; //class debug/Test
3: dup
4: ldc #11; //String
6: invokespecial #12; //Method "":(LJava/lang/String;)V
9: pop
10: return
}
明白了上面這些知識,我們來做一個小測試吧:
public class Test2 extends Test1
{
System.out.print("1");
}
Test2(){
System.out.print("2");
}
static{
System.out.print("3");
}
{
System.out.print("4");
}
public static void main(String[] args) {
new Test2();
}
}
class Test1 {
Test1(){
System.out.print("5");
}
static{
System.out.print("6");
}
}
試試看能清楚打印的順序嗎?如果沒有new Test2()將打印什麼?