title: Why must delegation to a different constructor happen first in a Java constructor?
body: <p>In a constructor in Java, if you want to call another constructor (or a super constructor), it has to be the first line in the constructor.  I assume this is because you shouldn't be allowed to modify any instance variables before the other constructor runs.  But why can't you have statements before the constructor delegation, in order to compute the complex value to the other function?  I can't think of any good reason, and I have hit some real cases where I have written some ugly code to get around this limitation.</p>
  
  <p>So I'm just wondering:</p>
  
  <ol>
  <li>Is there a good reason for this limitation?</li>
  <li>Are there any plans to allow this in future Java releases?  (Or has Sun definitively said this is not going to happen?)</li>
  </ol>
  
  <hr>
  
  <p>For an example of what I'm talking about, consider some code I wrote which I gave in <a href="https://stackoverflow.com/questions/474535/best-way-to-represent-a-fraction-in-java/474612#474612">this StackOverflow answer</a>.  In that code, I have a BigFraction class, which has a BigInteger numerator and a BigInteger denominator.  The "canonical" constructor is the <code>BigFraction(BigInteger numerator, BigInteger denominator)</code> form.  For all the other constructors, I just convert the input parameters to BigIntegers, and call the "canonical" constructor, because I don't want to duplicate all the work.</p>
  
  <p>In some cases this is easy; for example, the constructor that takes two <code>long</code>s is trivial:</p>
  
  <pre><code>  public BigFraction(long numerator, long denominator)
    {
      this(BigInteger.valueOf(numerator), BigInteger.valueOf(denominator));
    }
  </code></pre>
  
  <p>But in other cases, it is more difficult.  Consider the constructor which takes a BigDecimal:</p>
  
  <pre><code>  public BigFraction(BigDecimal d)
    {
      this(d.scale() &lt; 0 ? d.unscaledValue().multiply(BigInteger.TEN.pow(-d.scale())) : d.unscaledValue(),
           d.scale() &lt; 0 ? BigInteger.ONE                                             : BigInteger.TEN.pow(d.scale()));
    }
  </code></pre>
  
  <p>I find this pretty ugly, but it helps me avoid duplicating code.  The following is what I'd like to do, but it is illegal in Java:</p>
  
  <pre><code>  public BigFraction(BigDecimal d)
    {
      BigInteger numerator = null;
      BigInteger denominator = null;
      if(d.scale() &lt; 0)
      {
        numerator = d.unscaledValue().multiply(BigInteger.TEN.pow(-d.scale()));
        denominator = BigInteger.ONE;
      }
      else
      {
        numerator = d.unscaledValue();
        denominator = BigInteger.TEN.pow(d.scale());
      }
      this(numerator, denominator);
    }
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>There have been good answers, but thus far, no answers have been provided that I'm completely satisfied with, but I don't care enough to start a bounty, so I'm answering my own question (mainly to get rid of that annoying "have you considered marking an accepted answer" message).</p>
  
  <p>Workarounds that have been suggested are:</p>
  
  <ol>
  <li>Static factory.
  
  <ul>
  <li>I've used the class in a lot of places, so that code would break if I suddenly got rid of the public constructors and went with valueOf() functions.</li>
  <li>It feels like a workaround to a limitation.  I wouldn't get any other benefits of a factory because this cannot be subclassed and because common values are not being cached/interned.</li>
  </ul></li>
  <li>Private static "constructor helper" methods.
  
  <ul>
  <li>This leads to lots of code bloat.</li>
  <li>The code gets ugly because in some cases I really need to compute both numerator and denominator at the same time, and I can't return multiple values unless I return a <code>BigInteger[]</code> or some kind of private inner class.</li>
  </ul></li>
  </ol>
  
  <p>The main argument against this functionality is that the compiler would have to check that you didn't use any instance variables or methods before calling the superconstructor, because the object would be in an invalid state.  I agree, but I think this would be an easier check than the one which makes sure all final instance variables are always initialized in every constructor, no matter what path through the code is taken.  The other argument is that you simply can't execute code beforehand, but this is clearly false because the code to compute the parameters to the superconstructor is getting executed <em>somewhere</em>, so it must be allowed at a bytecode level.</p>
  
  <p>Now, what I'd like to see, is some <em>good</em> reason why the compiler couldn't let me take this code:</p>
  
  <pre><code>public MyClass(String s) {
    this(Integer.parseInt(s));
  }
  public MyClass(int i) {
    this.i = i;
  }
  </code></pre>
  
  <p>And rewrite it like this (the bytecode would be basically identical, I'd think):</p>
  
  <pre><code>public MyClass(String s) {
    int tmp = Integer.parseInt(s);
    this(tmp);
  }
  public MyClass(int i) {
    this.i = i;
  }
  </code></pre>
  
  <p>The only real difference I see between those two examples is that the "<code>tmp</code>" variable's scope allows it to be accessed after calling <code>this(tmp)</code> in the second example.  So maybe a special syntax (similar to <code>static{}</code> blocks for class initialization) would need to be introduced:</p>
  
  <pre><code>public MyClass(String s) {
    //"init{}" is a hypothetical syntax where there is no access to instance
    //variables/methods, and which must end with a call to another constructor
    //(using either "this(...)" or "super(...)")
    init {
      int tmp = Integer.parseInt(s);
      this(tmp);
    }
  }
  public MyClass(int i) {
    this.i = i;
  }
  </code></pre>