public class SampleClass
{
int a = 10; // loaded into memory for every object creation
int b = 11; // loaded into memory for every object creation
ExtraClass extraClass = new ExtraClass(); // loaded into memory for every object creation
static int c; // loaded when class is loaded into memory
{
// IIB
System.out.println(a);
System.out.println(b);
System.out.println(extraClass);
}
public SampleClass()
{
super();
// call to IIB
System.out.println("===============");
System.out.println(a);
System.out.println(b);
System.out.println(extraClass);
}
public static void main(String[] args)
{
SampleClass obj = new SampleClass();
}
}
class ExtraClass
{
}
我知道a
和b
只有在創建對象的時候才加載到SampleClass
。但是我正在檢查我的代碼的哪一部分被加載到內存中?我沒有從我的輸出中得出任何結論。代碼非靜態變量的哪部分被加載到內存中?
10
11
[email protected]
===============
10
11
[email protected]
適用[JLS中的一部分(http://docs.oracle.com/javase/specs/jls/se8/html/jls-12。 html#jls-12.5) – RealSkeptic