5

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.

0

5 Answers 5

9

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]

Sign up to request clarification or add additional context in comments.

1 Comment

The statement about usage of the constructor chaining is true for most API. But i can recall when i last saw passing a null in some of those API. If a object can be constructed without some value, generally is should not be part of constructor.
1

Case 3, provide a method that will create the object.

public class A { public static A create(B b) { A a = new A(); a.setB(b); } public static A create(B b, C c) { A a = create(b); a.setC(c); return a; } private A() {} public void setB(B); public void setC(C); } 

The constructor should be able to crate the object to prevent memory leaks. Less parameters in it minimize the situation when NullPointerException will occur. Providing a factory method like presented would decrease that risk and improve the control over object.

10 Comments

Chances are that an A object without either a B or a C object is invalid. I'd rather have that an invalid A can not be created (even if it's only inside a factory method).
how is this preferable to using constructors?
@djna, That depend of the object that is created this way. In case when during creation something might go wrong it should be not initialized via constructor. If Constructor only assign references to members, then this solution should not be used.
@JoachimSauer, if I understood you properly, we may want to add some asserts before instance creation and as constructor of A is private it can not be accessed from other places so instance of A it can be created only via factory method.
@Vash If something can go wrong during construction we can throw an Exception. I see no difference in error handling possibilities in this "factory" case and normal constructor. I'm having trouble seeing advantage of the factory idiom.
|
1

Case one is to prefer. This is done in most classes in the API that has this option. It also means that you can change the second/third/... argument in future releases if needed without having to upgrade the calls to the constructor.

Comments

0

The first case is the best one. Why would you want to avoid having multiple constructors? Unless you are not callig them, they do not bother you. Having different constructors also increases the readability of your code, because it is clear what parameters are needed to instantiate the object, in contrast to the null-case, where you always have to look what parameter is not needed.

Comments

0

Nulls are not typed, hence you create potential for future errors and doubt

 new Thing( myA, null, null) 

it's not clear here that the intent is to pass a null B and a null C,

 new Thing (myA) 

is unambiguous

2 Comments

In this case I can always typecast Null.
@shantanu, you can but there is no compulsion to do so, so you, or a less diligent person will not and then we get less maintainable code. The class author, by not providing separate constructors has given the client more work to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.