title: How ThreadLocal variable passes copy of its object to different Threads in java?
body: <p>Below are the example of creating the Thread Local variable</p>
  
  <pre><code>private static final ThreadLocal&lt;SimpleDateFormat&gt; formatter = new ThreadLocal&lt;SimpleDateFormat&gt;(){
      @Override
      protected SimpleDateFormat initialValue()
      {
          return new SimpleDateFormat("yyyyMMdd HHmm");
      }
  };
  </code></pre>
  
  <p>And Also Second way to use Thread Local</p>
  
  <pre><code>public class MyThreadLocal {
  
      public static final ThreadLocal userThreadLocal = new ThreadLocal();
  
      public static void set(Context user) {
          userThreadLocal.set(user);
      }
  
      public static void unset() {
          userThreadLocal.remove();
      }
  
      public static Context get() {
          return userThreadLocal.get();
      }
  }
  </code></pre>
  
  <p>Now,The below line create a thread local variable and it is static.</p>
  
  <pre><code>public static final ThreadLocal userThreadLocal = new ThreadLocal();
  </code></pre>
  
  <p>Static variables are initialized whenever class is loaded in JVM. That means one object is created When the class loads.</p>
  
  <p>*<strong>*Question:
  *<em>When different Thread calls Set or get on ThreadLocal variable how is the copy of threadlocal object is passed to the thread?</em>*How each Thread gets its own object?</strong>
  If one object is created when class is loaded ,then how is this happening?**</p>