Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call one constructor from another constructors in one class

I've encountered the following question online.

If we call one constructor from another in a class, what will happen?

Can anyone give me some hints?

like image 920
Fihop Avatar asked Feb 22 '26 16:02

Fihop


1 Answers

in java also its possible with the power of the this keyword. check out the example given below.

public class A {

    public A() {
        //this("a");
        System.out.println("inside default Constructor");
    }

    public A(String a){
        this();
        System.out.println("inside Constructor A");
    }

}

This concept is called constructor chaining. If it's c# i found this saying it's possible Is nesting constructors (or factory methods) good, or should each do all init work

like image 183
Mightian Avatar answered Feb 27 '26 03:02

Mightian