Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Please have a look at following snippet:

public class Foo {
int digit;
String name;

Foo (int d, String n) {
    this(d);
    // cannot do following.
    //compile-time error: Constructor call must be first statement in a Constructor 
    //this(n);
}

Foo (int p) {
    digit = p;
}

Foo (String q) {
    name = q;
}

What would be the reason of this restriction? I understand why call to Constructor (both in a Constructor or in a method) has to be first statement - the object should be initialized properly before it is used. What I don't get is compiler not allowing calling multiple constructors of the same class from within a constructor. I don't see any problem with this construct - both regarding initialization and invarients.

It'd be great if someone can shed some light on what I am missing here...

Thanks!

share|improve this question

3 Answers 3

up vote 6 down vote accepted

Constructors are not just "methods that are called when object is created", they are conceptually different.

Constructor's purpose is to constrain what states objects can be in initially. A freshly created object is zeroed on all fields (null/0/false) and that may be an invalid state in your program. For example, a Customer object in your application needs be constrained to always have a customerId > 0 — you then define a constructor that takes that id and checks it.

In this light there is no purpose in calling two constructors: after calling the first one the object is already in a consistent state. The second constructor will receive an initialized object which is a breach of its purpose (doing 0 => initialized).

What you want to do in this case is either introduce private methods that will do parts of that initialization and call them or make more specific constructors call the more general one (which is a very common pattern):

Foo (int d, String n) {
    digit = d;
    name = n;
}

Foo (int p) {
    this(p, null)
}

Foo (String q) {
    this(0, q)
}
share|improve this answer
    
thanks, that makes sense... never thought from this angle. :) –  Chaitanya Feb 23 '12 at 8:59

The reason is that the Java Language Specification says so. It could probably have been written to allow what you describe, but not for free: enforcing the order makes things simpler. Think about inheritance and calls to super(). If it wouldn't be the first statement in a constructor, then the subclass could do something with a superclass which is not yet constructed. At what point would this exist if the ubiquitous superclass Object wouldn't have yet been constructed?

Allowing arbitrary constructor call order would't have been very useful, but it would have complicated the language spec and compiler (a lot, I guess).

share|improve this answer
    
thanks for the answer, this restriction does make code consistent.. –  Chaitanya Feb 23 '12 at 9:00

Calling a constructor from with in itself would become recursive.

share|improve this answer
    
If a this() call is recursive, wouldn't it be recursive regardless of if it is the first or last statement in the constructor? –  Joonas Pulakka Feb 23 '12 at 8:34
1  
This is not the case here. –  Fixpoint Feb 23 '12 at 8:37
    
Calling the same constructor would overflow the stack, but in this case the OP wants to call a different overloaded constructor. Still not allowed but for different reasons covered by Fixpoint. –  MattDavey Feb 23 '12 at 10:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.