title: How to declare global variables using TestNG/Selenium/Java?
body: <p>I am totally new in automated testing.
  For practice purposes, I want to create tests for the contact form in Selenium with TestNG. <a href="http://automationpractice.com/index.php" rel="nofollow noreferrer">This is the page</a> I am using for practice. I created several test cases but I am not sure how to declare variables that I will be calling later on (in the same class). The code is below, I would like to declare 'Email', 'ErrorField' and 'SendButton' - all suggestions are much appreciated since I tried several ways and I am getting errors.</p>
  
  <pre><code>public class FormValidation {
    protected static WebDriver driver;
  
    @BeforeTest()
    public void beforeTest() {
      System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
    }
  
    @Test(priority = 0)
    public void blankFormTest() {
      driver = new ChromeDriver();
      driver.get("http://automationpractice.com/index.php?controller=contact");
  
      WebElement SendButton = driver.findElement(By.id("submitMessage"));
      SendButton.click();
      WebElement ErrorField = driver.findElement(By.xpath("//*[@id=\"center_column\"]/div/ol/li"));
      {
        Assert.assertEquals(ErrorField.getText(), "Invalid email address.");
  
      }
    }
  
    @Test(priority = 1)
    public void correctEmailonly() {
      WebElement Email = driver.findElement(By.id("email"));
      Email.sendKeys("kasiatrzaska@o2.pl");
      WebElement SendButton = driver.findElement(By.id("submitMessage"));
      SendButton.click();
      WebElement ErrorField = driver.findElement(By.xpath("//*[@id=\"center_column\"]/div/ol/li"));
      {
        Assert.assertEquals(ErrorField.getText(), "The message cannot be blank.");
      }
  
    }
  }
  </code></pre>