If I am calling another constructor of same class using this keyword. Will it create two objects?
If so, which object will be active in below example:
Class Sample {
int a;
String b;
Sample() {
this("Hello");
a=10;
}
Sample(String temp) {
b = temp;
}
}
I want to know the behaviour of Constructor chaining. Please explain how it works?
No, constructor chaining doesn't create an additional instance, it just executes the logic of the chained constructor, allowing you to reuse its code.
No,
this("Hello"); here ll call Sample(String temp) constructor, it won't create another object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With