views:

284

answers:

6

Hi This was the question asked in interview. Can we call one constructor from another if a class has multiple constructors in java and when?How can I call I mean syntax?

+5  A: 
this(other, args);
Oli
thats more what i was trying to do haha
Woot4Moo
This is either misleading or just plain wrong! What is other? You simply invoke the other constructor using `this(args-required-by-constructor)` or `super(args-required-by-constructor);`
Software Monkey
Chill out. Don't take things so literally. *Most* people here figured out "other, args" refers to the other arguments of the other constructor.
Oli
+3  A: 

example:

public class FileDb {

  /**
   * 
   */
  public FileDb() {
    this(null);
  }

  public FileDb(String filename) {
    // ...
  }

}
Synox
+11  A: 

You can, and the syntax I know is

this(< argument list >);

You can also call a super class' constructor through

super(< argument list >);

Both such calls can only be done as the first statement in the constructor (so you can only call one other constructor, and before anything else is done).

Sean Nyman
And you can't do anything that requires a reference to this.
Software Monkey
it also must be in the first line of the constructor
sixtyfootersdude
+4  A: 

Yes, you can do that.

Have a look at the ArrayList implementation for example:

public ArrayList(int initialCapacity) {
    super();
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    this.elementData = new Object[initialCapacity];
}

/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    this(10);
}

The second constructor calls the first one with a default capacity of ten.

Peter Lang
+4  A: 

FYI, this is called the telescoping/telescopic constructor pattern.

It's discussed in JLS 8.8.7.1 Explicit Constructor Invokations

  • Alternate constructor invocations begin with the keyword this (possibly prefaced with explicit type arguments). They are used to invoke an alternate constructor of the same class.
  • Superclass constructor invocations begin with either the keyword super (possibly prefaced with explicit type arguments) or a Primary expression. They are used to invoke a constructor of the direct superclass.
polygenelubricants
+3  A: 

None of the answers are complete, so I'm adding this one to fill in the blanks.

You can call one constructor from another in the same class, or call the super class, with the following restrictions:

  1. It has to be the first line of code in the calling constructor.
  2. It cannot have any explicit or implicit reference to this. So you cannot pass an inner class (even an anonymous one if it references any instance methods), or the result of a non-static method call, as a parameter.

The syntax (as mentioned by others) is:

MyClass() {
   someInitialization();
}

MyClass(String s) { 
     this();
     doSomethingWithS(s);
}
Yishai