title: Get ThreadLocal Variable In Different Classes - Java - Selenium WebDriver
body: <p>How do I get use a ThreadLocal value that was set in a different class?</p>
  
  <p>Example, I set these variables in class A:</p>
  
  <pre><code>ThreadLocal&lt;String&gt; username = new ThreadLocal&lt;String&gt;();
  username.set("user");
  
  ThreadLocal&lt;String&gt; password = new ThreadLocal&lt;String&gt;();
  password.set("pass");
  </code></pre>
  
  <p>I know how to "get" them from within the same class by doing:</p>
  
  <pre><code>String myUsername = username.get();
  String myPassword = password.get();
  </code></pre>
  
  <p>But I can't find anything on how I would use/get them in Class B?</p>
  
  <p>The basic end goal is I am trying to convert my Selenium WebDriver tests to run in parallel. These text values will get entered in one class (A), but then I have another class (B) that contains "methods" that get called upon (check database entries, verify on screen data, etc.) that would need to know the data that was entered in class (A).</p>
  
  <p>The way I am doing this for non-ThreadLocal variables successfully is like this:</p>
  
  <p>Set the value in class A:</p>
  
  <pre><code>@Test (priority = 1)
  public void enterData() {
  
      String environment = "QA";
  
      // Pass environment to stored variables
      StoredVariables newEnv = new StoredVariables();
      newEnv.setEnv(environment);
  
      driver.enterData;
  
  }
  </code></pre>
  
  <p>Store it in class B (StoredVariables):</p>
  
  <pre><code>public class StoredVariables {
  
      private static String environment = "";
  
      public StoredVariables() {
  
      }
  
      public static String getEnv() {return environment;}
      public void setEnv(String s) {environment = s;}
  
  }
  </code></pre>
  
  <p>Access it from class C:</p>
  
  <pre><code>@Test (priority = 1)
  public void verifyData() {
  
      String environment = StoredVariables.getEnv();
  
  }
  </code></pre>