title: Java update global variable between classes
body: <p>I have a public static string called randomWord. After a different class runs a method I want the "value" of it to change. An example: Class Variables having the public static String randomWord; Class 1 does some stuff and sets Variables.randomWord = orange. Class 2 needs to get the new randomWord. What's a possible method for updating the randomWord in class variable?</p>
  
  <p>Thanks in advance</p>
  
  <pre><code>    public final class Variables
  {
      public static String randomWord;
  }
  </code></pre>
  
  <p>Class ^</p>
  
  <pre><code>    public static String pickWord()
          throws IOException
  {    
      File words = new File("wordList.txt");
      String wordToArray;
      String[] arrWord = new String[3863];   
      Scanner sc = new Scanner(words);
      Random rWord = new Random();
      int i = 0;
  
      do
      {
          sc.nextLine();
          wordToArray = sc.next();
          arrWord[i] = wordToArray;
          i++;    
      }while(sc.hasNext());
  
      Arrays.toString(arrWord);
      int idx = rWord.nextInt(arrWord.length);
  
      Variables.randomWord = (arrWord[idx]);
      System.out.println(Variables.randomWord);
      return Variables.randomWord;
  }
  </code></pre>
  
  <p>Where I change the value, set the .txt document of words into the array, and randomly get a word from the array. Class 1</p>
  
  <pre><code>String selectedWord = Variables.randomWord;
              System.out.println("Got your word, get ready!" + selectedWord);
              return selectedWord;
  </code></pre>
  
  <p>where I need the new value Class 2</p>