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.

4

9 回答 9

4

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();
    }
}
于 2013-01-03T08:00:35.437 回答
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();
}
于 2013-01-03T08:05:36.727 回答
1

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

C(int i, String s)
{
   this();
   // other stuff.
}
于 2013-01-03T07:58:45.463 回答
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.

于 2013-01-03T08:00:09.503 回答
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();
}
于 2013-01-03T08:46:16.467 回答
0

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

于 2013-01-03T07:57:42.447 回答
0

You can use this(); to call default constructor

C(int i, String s){
   this(); // call to default constructor
   // .....       
}
于 2013-01-03T07:59:08.460 回答
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?
}
于 2013-01-03T07:59:53.077 回答
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

于 2013-01-03T08:00:51.157 回答