title: How do I declare a driver as global?
body: <p>Here is how I declare firefox driver: </p>
  
  <pre><code>public static WebDriver driver = new FirefoxDriver();
  </code></pre>
  
  <p>I place the code above outside main and within my class (global) </p>
  
  <p>Here is how I declare chrome driver: </p>
  
  <pre><code>System.setProperty("webdriver.chrome.driver", "/path/xxx/xxx/xx");
  WebDriver driver  = new ChromeDriver();
  </code></pre>
  
  <p>I place the code above in main</p>
  
  <p>Here is the issue:</p>
  
  <p>I want to make the ChromeDriver as a global but I <strong>NEED</strong> to set the property <strong>before</strong> doing so. But I place the <code>System.setProperty("xx","xx");</code> within the main body. Cuz it gives error when placed outside.</p>
  
  <p>Here is a user trying to do the same thing as me. Trying to run different browsers using the same driver : <a href="https://stackoverflow.com/questions/11823150/how-to-initialize-multiple-browsers-in-selenium-webdriver-a-k-a-selenium-2-usi">How to run Selenium tests in multiple browsers for cross-browser testing using Java?</a></p>
  
  <p>The answer is involves declaring the driver in the main body and not as a constant before.</p>
  
  <p><strong>My issue:</strong> All functions need driver declaration from before. Calling functions which use <code>driver</code>. If I declare <code>driver</code> in main, I need to continuously pass it as a parameter to all the functions. I do not wish to do that. Here is an example function</p>
  
  <pre><code> public static void a(){
  
   driver.findElement(By.id("hi"));
  
  }
  </code></pre>