35

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?

sixtyfootersdude
  • 25,859
  • 43
  • 145
  • 213
giri
  • 26,773
  • 63
  • 143
  • 176
  • Can you clarify the question? Multiple constructors are there to create the objects differently and can be called from other classes. They are made just for that purpose. – Teja Kantamneni Mar 03 '10 at 18:44
  • 4
    he means calling a constructor from another one. – Woot4Moo Mar 03 '10 at 18:45
  • he's asking whether it is allowed to call a constructor from another constructor or not whenever a class has more than one constructor – Francis Jun 28 '15 at 16:07

6 Answers6

42

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
  • 4,450
  • 25
  • 22
11

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
  • 54,264
  • 27
  • 148
  • 161
10

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
  • 90,445
  • 31
  • 189
  • 263
8

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
  • 376,812
  • 128
  • 561
  • 623
7
this(other, args);
Oli
  • 235,628
  • 64
  • 220
  • 299
  • 3
    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);` – Lawrence Dol Mar 03 '10 at 19:15
6

example:

public class FileDb {

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

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

}
Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
Synox
  • 1,148
  • 2
  • 17
  • 38