title: Proper way to update static variable periodically
body: <p>I have a static variable that I load at the beginning of a class. I want to update the variable every hour. The question is what is the proper way of doing this?</p>
  
  <p>The way I am trying to do it is the following but it requires the method that updates the static variable to be in every constructor:</p>
  
  <pre><code>import java.util.Date;
  
  public class MyClass {
  
      private static String globalString = "";
  
      // initialize lastUpdate with two hours back to make sure first update happens
      private static Date lastUpdate = new Date(System.currentTimeMillis() - (2 * (3600 * 1000)));
  
  
      MyClass() {
          updateGlobalString();
  
          // DO MORE STUFF HERE...
      }
  
  
      MyClass(String string) {
          updateGlobalString();
  
          // DO MORE STUFF HERE...
      }
  
      private synchronized void updateGlobalString() {
          // check if we need to update
          if (lastUpdate.before(new Date(System.currentTimeMillis() - (3600 * 1000)))) {
  
              // DO THINGS TO UPDATE globalString HERE...
  
              lastUpdate = new Date();
          }
      }
  }
  </code></pre>
  
  <p>Any other ideas/better way of doing it?</p>