Kundan Kumar
Kundan Kumar

Reputation: 9

redundant null check in eclipse kepler

I have following line of code in eclipse kepler.

if (this != null) {

}

This != null show an error "Redundant null check: this expression cannot be null" even if i have changed Redundant null check into warning by going through Java > Compiler > Errors/Warnings > Null analysis.

Upvotes: 0

Views: 1389

Answers (1)

helb
helb

Reputation: 7773

Considering the following:

  • The keyword this can only be used inside an instance method, i.e. in a method which is executed on an existing object.
  • this refers to the object on which the method is called
  • this is read-only (cannot explicitly assign null to it for example).
  • Calling an instance method on a null object would result in an exception.

Therefore, it is guaranteed that this is never null.

The compiler warning helps you to identify code which makes no sense or code which is never be executed.

EDIT: About error / warning behavior of eclipse kepler, see eclipse-kepler-shows-error-marks-on-warnings

Upvotes: 3

Related Questions