title: Thread scope in java for object instance
body: <p>I understand that objects are stored in heap space. with more details here :
  <a href="https://www.hackerearth.com/practice/notes/runtime-data-areas-of-java/" rel="nofollow noreferrer">https://www.hackerearth.com/practice/notes/runtime-data-areas-of-java/</a></p>
  
  <p>in the following code the object <code>param</code> the reference of it is stored in thread stack, but the object itself is stored in heap :</p>
  
  <pre><code>private void foo(Object param) { 
          ....
      } 
  </code></pre>
  
  <p>To ask my question , At first I will start with code :</p>
  
  <pre><code>public class Thread1 implements Runnable {
  Test test = new Test();
  
  public void run() {
      test=new Test(); // This will affect other thread , the object reference is changed here
      System.out.println(test.id());
     }
      }
  </code></pre>
  
  <p>in the code above all the threads from the same instance of <code>Thread1</code> will have the same reference( let's say variable) of <code>test</code> , which means changing the reference of test will affect other thread:</p>
  
  <pre><code>  Runnable runnable=new Thread1();
      Thread thread1=new Thread(runnable);
      thread1.start();
      Thread t2=new Thread(runnable);
      thread2.start();
  </code></pre>
  
  <p>The question here , <code>test</code> will be stored in heap. but how does thread access it ? ( I don't think it will have reference in stack, because changing the value inside thread won't affect the other in this case). if the thread can access that variable directly ( let's say no reference in stack) what scope will it have ? ( I mean shouldn't it be limited to it's own variable )</p>