DRAFT
Observed
In a constructor, this is null
In a constructor, this is never null
Already at the beginning of the constructor,
this
refers to the new (still uninitialized) object.
If this
was null
in the constructor,
one couldn’t initialize the object,
because one would not have any reference to the object.
The following exam question, while having the goal of checking whether students understand that constructors don’t return anything, also brings up this misconception:
Is the constructor implementation in the following class correct? Explain!
public class C {
public C() {
return this;
}
}
Example answer: “No, because this does not refer to anything.”