首先簡單的介紹一下JVM簡介
ClassLoader
通過下面代碼我們引入話題.
這段代碼運行出的結果是什麼?
public class Test3 {
public static void main(String[] args){
t test3 = t.getTest();
System.out.println(test3.anInt);
System.out.println(test3.anInt1);
}
}
class t {
public static int anInt ;
public static int anInt1 = 0;
private static t test = new t();
private t() {
anInt++;
anInt1++;
}
public static t getTest(){
return test;
}
}
1
1
如果是這樣的,那麼運行結果是什麼樣子的呢?
class t {
private static t test = new t();
public static int anInt ;
public static int anInt1 = 0;
private t() {
anInt++;
anInt1++;
}
public static t getTest(){
return test;
}
1
0
爲什麼會出現這種情況呢?跟什麼有關係
- 跟類加載器有關
- 程序執行順序有關