Let there be a class A
. Class B
is as follows:
public class B {
private int anInt;
private String aString;
...
public B(A initObj) {
anInt = initObj.getIntField();
aString = initObj.getStringField();
...
}
...
}
If initObj is null
, then what makes the most sense is for the B
object to be null also. But, according to the internet, a Java constructor cannot return a null.
Obviously, it is best to check on the side of the code that is calling the constructor for B
so as not to call the constructor with a null argument. But, assuming that this possibility occurs, what is the best way to gracefully handle this situation.
What is the best way to handle a situation where B
is initialized with null
?