If a class need multiple field information during object creation and also it is allowing fewer information.Than we have two option
1. Provide multiple constructor, or
2. Allow client to pass null argument while creating object.
Among these which is the best practice.
ex:
Case-1:
public class Test {
Test(A ob1,B ob2, C ob3){
}
Test(A ob1,B ob2){
this(ob1, ob2, null);
}
public static void main(String args[]){
Test ob = new Test(new A(),new B());
}
}
Case-2:
public class Test {
Test(A ob1,B ob2, C ob3){
}
public static void main(String args[]){
Test ob = new Test(new A(),new B(), null);
}
}
I have used main method in same class. Please consider these main methods in some other class.
Using multiple constructors is the best. Many standard API libraries also have implemented it.
Also It makes code loosely coupled and not a good practice to create the object by passing explicit ‘null’ value.
Case 2 increases chances of High coupling.
Apart from main question :
Read More: Best way to handle multiple constructors in Java
EDIT:
Its better practice to use named factory methods to construct objects as they’re more self-documenting than having multiple constructors. [Effective Java by Joshua Bloch]