While the object is being constructed, can it be null ?
class Sample {
public Sample() {
if( this != null ) { // Is this check necessary anywhere in the constructor?
this.doSomething();
}
}
....
....
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
thiswon’t be null – but if you call a non-final instance method from a constructor, you should document that really thoroughly, as any subclass constructors won’t have been run yet. That means ifdoSomething()is overridden in a subclass, it will see the default values for any fields declared in that subclass (where the default is the default value for the type, not whatever the variable initializer might show). Basically it’s worth trying to avoid calling non-final instance methods in constructors if at all possible.