1

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.

Community
  • 1
  • 1
mtk
  • 13,221
  • 16
  • 72
  • 112

9 Answers9

5

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();
    }
}
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
  • So, I should do a copy/paste of the code from the default constructor in order to achieve what I need? – mtk Jan 03 '13 at 08:04
  • Yes, if you want call another constructor after completing work in current constructor, since that cannot be achieved with this(). – Pradeep Simha Jan 03 '13 at 08:05
  • Try to avoid doing copy/paste where possible - it's not a good habit to get into and will give you headaches later. You could always [delegate to an init method in your constructors to avoid that, or switch how your constructors do initialization](http://stackoverflow.com/a/14135083/836214). – Krease Jan 03 '13 at 08:08
  • @mtk, in that case, you need to re-think the logic, copy paste is not good – Pradeep Simha Jan 03 '13 at 08:09
2

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();
}
Krease
  • 15,805
  • 8
  • 54
  • 86
1

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

C(int i, String s)
{
   this();
   // other stuff.
}
Juvanis
  • 25,802
  • 5
  • 69
  • 87
1

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.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
1

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();
}
Yogesh Ralebhat
  • 1,376
  • 1
  • 13
  • 29
0

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

0

You can use this(); to call default constructor

C(int i, String s){
   this(); // call to default constructor
   // .....       
}
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
0

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?
}
Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
0

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

Rahul
  • 15,979
  • 4
  • 42
  • 63