Memory allocation and sharing of the reference that is passed to the classes in Java
<p>I am confused with the memory allocation of references in Java when you pass them to the methods in Java. As a Test I did an example as below,</p>

<pre><code>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());   
   }
 }
</code></pre>

<p>Well the output is 100.</p>

<p>Here I am passing the Store reference to ProducerStore. Inside ProducerStore I am assigning it to the class member of it. I am doing the same for ConsumerStore as well.</p>

<p>Could someone explain me how the memory allocation is done for Store referece and how it shared among the ProducerStore and ConsumerStore?</p>
