Question

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!

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top