title: Problems initializing a final variable in Java
body: <p>I keep running into slight variations of a problem in Java and it's starting to get to me, and I can't really think of a proper way to get around it.</p>
  
  <p>I have an object property that is final, but dynamic. That is, I want the value to be constant once assigned, but the value can be different each runtime. So I declare the class level variable at the beginning of the class - say <code>private final FILE_NAME;</code>. Then, in the constructor, I assign it a value - say <code>FILE_NAME = buildFileName();</code></p>
  
  <p>The problem begins when I have code in the <code>buildFileName()</code> method that throws an exception. So I try something like this in the constructor:</p>
  
  <pre><code>try{
     FILE_NAME = buildFileName();
  }
  catch(Exception e){
     ...
     System.exit(1);
  }
  </code></pre>
  
  <p>Now I have an error - "The blank final field FILE_NAME may not have been initialized." This is where I start to get slightly annoyed at Java's strict compiler. I know that this won't be a problem because if it gets to the catch the program will exit... But the compiler doesn't know that and so doesn't allow this code. If I try to add a dummy assignment to the catch, I get - "The final field FILE_NAME may already have been assigned." I clearly can't assign a default value before the try-catch because I can only assign to it once.</p>
  
  <p>Any ideas...?</p>