Javaのメソッドに参照を渡すとき、Javaの参照のメモリ割り当てと混同しています。テストとして、以下の例を実行しました。
class Store{
public int[] buffer = new int[5];
}
class ConsumeStore{
Store store;
public ConsumeStore(Store store){
this.store = store;
}
public int useStore(){
return store.buffer[0];
}
}
class ProduceStore{
Store store;
public ProduceStore(Store store){
this.store = store;
}
public void putStore(){
store.buffer[0] = 100;
}
}
public class CheckRef {
public static void main(String args[]){
Store store = new Store();
ConsumeStore consStore = new ConsumeStore(store);
ProduceStore prodStore = new ProduceStore(store);
prodStore.putStore();
System.out.println(consStore.useStore());
}
}
さて、出力は100です。
ここでは Store 参照を ProducerStore に渡しています。ProducerStore 内で、それをクラス メンバーに割り当てています。私は ConsumerStore でも同じことをしています。
Store参照のためにメモリ割り当てがどのように行われ、ProducerStoreとConsumerStoreの間でどのように共有されるかを誰かに説明してもらえますか?