Any way to call the default constructor from a parameterized constructor?

11.4k Views Asked by At

Suppose, I have the following code

class C {
    int i;
    String s;

    C(){
        System.out.println("In main constructor");
        // Other processing
    }

    C(int i){
        this(i,"Blank");
        System.out.println("In parameterized constructor 1");
    }

    C(int i, String s){
        System.out.println("In parameterized constructor 2");
        this.i = i;
        this.s = s;
        // Other processing 
        // Should this be a copy-paste from the main contructor? 
        // or is there any way to call it? 
    }
    public void show(){
        System.out.println("Show Method : " + i + ", "+ s);
    }
}

I would like to know, Is there any way, I can call the main(default) constructor from the parametrized constructor (i.e. C(int i, String s) in this case) ?

Or I have just copy-paste the entire contents from the main(default) constructor to the parametrized one, as shown in comments in the above code?

Note

I need to call the default constructor after the variables i and s are set in the parametrized constructor, as the processing involves these variables.

Edit

I see this post, which says placing this() as the first line would call the default constructor. But I need to call it after the setting of values.

9

There are 9 best solutions below

4
Pradeep Simha On BEST ANSWER

Calling this() would work, but note this must be the first statement in constructor. For eg: Below code would be illegal and won't compile:

class Test {
    void Test() { }
    void Test(int i) {
        i = 9;
        this();
    }
}
0
AudioBubble On

Just call the constructor ny this(); statment as a first line in your default constructor....

0
Juvanis On

Calling this() as the first statement in other constructors is enough.

C(int i, String s)
{
   this();
   // other stuff.
}
0
Nandkumar Tekale On

You can use this(); to call default constructor

C(int i, String s){
   this(); // call to default constructor
   // .....       
}
0
Marcin Szymczak On

Use this() in first line of parametrized constructor

C(int i, String s){
    this();
    System.out.println("In parameterized constructor 2");
    this.i = i;
    this.s = s;
    // Other processing
    // Should this be a copy-paste from the main contructor?
    // or is there any way to call it?
}
0
Narendra Pathai On

Quoting the Docs:

the invocation of another constructor must be the first line in the constructor.

So no its not possible. Use this() in first line.

0
Rahul On

You can call this() in the first statement to call default constructor in any parameterized constructor.

Note this() should mandatorily be the first line in the constructor definition

C(int i, String s){
   this();
    . . . . 
}

But I need to call it after the setting of values.

It is not possible. constructor invocation has to be the first statement.

You can go through this link : constructor invocation must be the first line

0
Krease On

An option could be to call your parameterized constructor from your default constructor.

C(){
    this(0, "Blank");
}

C(int i){
    this(i,"Blank");
}

C(int i, String s){
    this.i = i;
    this.s = s;
}

This pattern will let you supply defaults for constructors with less arguments to the more specific constructors.

Also, note chaining constructors must be done as the first call in another constructor - you cannot call another constructor after initializing variables:

C(int i, String s){
    this.i = i;
    this.s = s;
    this();     // invalid!
}

If you really want to do something like this, consider an init method:

C() {
    init();
}
C(int i, String s) {
    this.i = i;
    this.s = s;
    init();
}
0
Yogesh Ralebhat On

You can move all the code from main constructor to some method, say, mainProcessing().

C()
{
    System.out.println("In main constructor");
    mainProcessing();
}

private void mainProcessing()
{
    // Move your code from main constructor to here.
}

Now in your parameterized constructor 2, you can call this method, mainProcessing(), at desired location.

C(int i, String s)
{
    System.out.println("In parameterized constructor 2");
    this.i = i;
    this.s = s;
    mainProcessing();
}