ThisInConstructorIsNull
DRAFT

Misconception:

In a constructor this is still null. It will only be set after the constructor finishes.

Incorrect

In a constructor, this is null

Correct

In a constructor, this is never null

Correction
Here is what's right.

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.

Symptoms
How do you know your students might have this misconception?

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.”

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.