1

I was studying this() keyword to invoke the constructors. I am pretty sure about its simple mechanism which is used to invoke the current class's other constructor:

public class AsCaller {
String name;
int id;

AsCaller() {
    System.out.println("No arguments");
}
AsCaller(String n, int i) {
    this();
    name = n;
    id = i;
}

void display() {
    System.out.println(id + " " +name);
}

public static void main(String[] args) {
    AsCaller example1 = new AsCaller("Student", 876);       
    example1.display();
  }
}

As expected it gives output of:

No arguments
876 Student

But, What if we have more than 2 constructors and some of them having 2 or more arguments. How will this() keyword will be used to invoke one of them? like:

public class AsCaller {
String name;
int id;

AsCaller(String city, String street, float house) {
    System.out.println("float datatype invoked");
}

AsCaller(int age, int marks) {
    System.out.println("int datatype invoked");
}

AsCaller(String n, int i) {
    // How to use this() keyword to invoke any of the above constructor?.
    this();
    name = n;
    id = i;
}

    AsCaller() {
    System.out.println("No arguments");
   }
void display() {
    System.out.println(id + " " +name);
}

public static void main(String[] args) {
    AsCaller example1 = new AsCaller("Student", 876);

    example1.display();
    }
}
HQuser
  • 640
  • 10
  • 26

3 Answers3

2

You can do this :

AsCaller(String n, int i) {
    // How to use this() keyword to call any of the above constructor?.
    this(1,i);// calls the AsCaller(int age, int marks) constructor
    name = n;
    id = i;
}

In a similar fashion, you need to pass arguments to this(..) which match the argument sequence of the constructor you want to call.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
2

Simply:

 this(age, marks);

Pass the arguments that match the constructor you want to call.

badroit
  • 1,316
  • 15
  • 28
1

I think it doesn't make sense to call two constructors from a constructor. The kind of constructors the coder coding the class chooses to provide is a design choice and it can be made so that you do not need to call two constructors from one constructor. I think the reason Java allows you to call one constructor from a constructor is since there might be some default initialization which the default constructor might do, like opening a socket, allocating memory etc which might be common across constructors. You could rewrite the code as follows so that you don't have to call two constructors from one constructor though the functionality is preserved.

    public class AsCaller {
    String name;
    int id;



    void intinit(int age, int marks){
        System.out.println("int datatype invoked");
    }
    void floatinit(String city, String street, float house){
        System.out.println("float datatype invoked");
    }
    AsCaller(String city, String street, float house) {
        floatinit(city,street,house);
    }

    AsCaller(int age, int marks) {
        intinit(age,marks);
    }

    AsCaller(String n, int i) {
      // How to use this() keyword to invoke any of the above constructor?.
        this();
        intinit(1,2);
        floatinit("a","b",3.0f);
        name = n;
        id = i;
    }

    AsCaller() {
        System.out.println("No arguments");
    }
    void display() {
        System.out.println(id + " " +name);
    }

    public static void main(String[] args) {
        AsCaller example1 = new AsCaller("Student", 876);

        example1.display();
    }  
    }

TL;DR: No, the compiler won't allow you to call two constructors from any constructor of the class since call to this must be first statement in constructor.

Sahil
  • 1,346
  • 1
  • 12
  • 17