title: Final variable from resources file
body: <p>I have an activity with some <strong>final</strong> variables. I extracted their values (let's assume they're all Strings) into a resources file. </p>
  
  <p>The problem:</p>
  
  <p>If I directly assign them on the instantiation (as following):</p>
  
  <p><code>private final String PREFERENCE_NAME = getResources().getString(R.string.preference_name);</code></p>
  
  <p>I get the following error:</p>
  
  <p><code>Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference</code></p>
  
  <p>I understand the problem; the onCreate() method has not been called yet which means I cannot access context-related methods (<code>getResources()</code>). </p>
  
  <p>If I want to assign the value in the onCreate() method of the activity, I get the error <code>Cannot assign value to final variable 'PREFERENCE_NAME'</code> </p>
  
  <p><strong>The question is:</strong> How can I get my final variables to be assigned from the resources file? And if this is not possible, what is the best practice for a solution? </p>
  
  <p>Thanks in advance.</p>