简体   繁体   中英

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

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 (ie 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.

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();
    }
}

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();
}

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

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

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.

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();
}

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

You can use this(); to call default constructor

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

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?
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM