Can “this” ever be null in Java?

No it can’t. If you’re using this, then you’re in the instance so this isn’t null.

The JLS says :

When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (ยง15.12), or to the object being constructed.

If you invoked a method from an object, then the object exists or you would have a NullPointerException before (or it’s a static method but then, you can’t use this in it).


Resources :

  • JLS – this keyword

Leave a Comment

tech