のすべてのインスタンスはrun
、メソッド ルックアップ テーブルへの同じ「ポインタ」を共有します。ただし、オブジェクトの正確なサイズは、run
およびその...
上の の内容によって異なります。コンパイラが生成する無名クラスは、run
メソッドによって参照される変数を格納するためのスペースを提供します。またthis
、外側のクラスのオブジェクトへの参照を格納するためのスペースも提供します。
例えば、
private String name;
void thisIsCalledManyTimes(){
final int value1 = 123;
final double value2 = 456.789.
someObject.executeIfNecessary( new Runnable(){
void run(){
System.out.println(name); // Referenced through the enclosing "this"
System.out.println(value1); // Variable will be inserted by the compiler
System.out.println(value2); // Variable will be inserted by the compiler
}
});
}
value1
、value2
、およびを格納するためにオブジェクト内のスペースを予約しますthis
。匿名オブジェクトの各インスタンスは、これらの変数のコピーを取得します。