title: Dynamically populate String's from a Properties file in Java
body: <p>Does anyone know of a method to load a properties file, and dynamically create Strings with identical names to the key value?</p>
  
  <p>I'm trying to clean up my code by moving all the system messages etc out of the logic and into a properties file, but want to avoid having to have a class consisting of dozens of lines like the following:</p>
  
  <pre><code>final String COMMS_ERROR = properties.getProperty(COMMS_ERROR);
  </code></pre>
  
  <p>An example of what I'm trying to achieve:</p>
  
  <pre><code>for (String key : properties.getPropertyValues()) {
      final String &lt;key&gt; = properties.getProperty(key)
  }
  </code></pre>
  
  <p>Obviously this won't work, the compiler will throw a fit. But I'm wondering if there's an elegant solution to do the same thing - create new Strings using the key names from the properties file - be it via a separate library or in my own code.</p>
  
  <p>One solution I've thought of is to populate a HashMap with the keys/values from the properties file, but then that would mean less elegant code in the form of:</p>
  
  <pre><code>import com.x.y.messages;
  ...
      throw new Exception(HM.get("COMMS_ERROR"));
  </code></pre>
  
  <p>Where HM is the HashMap located within com.x.y.messages...</p>
  
  <p>Ideally I just want to be able to do:</p>
  
  <pre><code>import com.x.y.messages;
  ....
      throw new Exception(COMMS_ERROR);
  </code></pre>
  
  <p>Any thoughts/advice appreciated.</p>