title: Why ThreadLocal variable need to static?
body: <p>I have read many articles on why <code>ThreadLocal</code> variable need to be static(although not necessary), but I didn't get the idea why it should be static.</p>
  
  <p><strong>I have read it <a href="https://stackoverflow.com/questions/2784009/why-should-java-threadlocal-variables-be-static">here</a> and many other links</strong> but didn't get the idea.</p>
  
  <p>I have done something like this</p>
  
  <pre><code>public class ThreadLocalDemo{
  
      public static void main(String[]args)throws Exception{
          SharedRersource r1= new SharedRersource();
          Thread t1= new Thread(r1);
          Thread t2= new Thread(r1);
          t1.start();
          t2.start();
          t1.join();
          t2.join();
          System.out.println("Main thread Exiting...");
          }
  
      }
  
  class SharedRersource implements Runnable{
  
  
          private ThreadLocal&lt;Integer&gt; threadId = new ThreadLocal(){
              protected Integer initialValue(){
                  return (int)(Math.random()*100);
                  }
          };
          public void run(){
  
              try{
                  Thread.sleep(2000);
              }
              catch(InterruptedException e){
                  e.printStackTrace();
                  }
                  System.out.println(threadId.get());
  
              }
          };
  </code></pre>
  
  <p><strong>Here thread t1 and t2 is having private copy of threadId than why It should be static</strong></p>
  
  <p>Please give a better understanding to me.
  Thanks </p>