A qualified this expression, written Outer.this, is the Java syntax for reaching the instance of an enclosing class from
inside an inner class. It is only needed when a plain this would refer to something else.
When the qualifier names the class that the expression is already lexically inside, and no inner class sits between the expression and that class,
Outer.this and this denote the very same object. The qualifier therefore adds no information.
This hurts readability in two ways. The expression is longer than it needs to be, and, more importantly, it tells the reader that an enclosing instance is involved when none is. Anyone reading the code has to look for a surrounding class to understand which object is being accessed, only to discover that it is the obvious one.
Lambdas are the case developers most often get wrong. A lambda body does not introduce its own this: inside a lambda,
this is still the instance of the class where the lambda is written. A qualified this in a lambda declared directly in the
class it names is just as redundant as one written in a method body.
To fix an issue, delete the qualifier and keep the plain this. The expression keeps referring to the same object, so the change cannot
alter behavior.
Remove the class name and the dot in front of this, leaving a plain this. If the member access is not ambiguous either,
for example because no local variable or parameter shadows the field, you may drop this as well, but keeping it is enough to resolve the
issue.
public class Invoice {
private String id;
private List<String> lines = new ArrayList<>();
public String describe() {
return "Invoice " + Invoice.this.id; // Noncompliant: "Invoice.this" is the same object as "this"
}
public List<String> nonEmptyLines() {
return Invoice.this.lines.stream() // Noncompliant: "Invoice.this" is the same object as "this"
.filter(line -> !line.isEmpty() && line.startsWith(Invoice.this.id)) // Noncompliant: a lambda does not introduce its own "this"
.collect(Collectors.toList());
}
}
public class Invoice {
private String id;
private List<String> lines = new ArrayList<>();
public String describe() {
return "Invoice " + this.id;
}
public List<String> nonEmptyLines() {
return this.lines.stream()
.filter(line -> !line.isEmpty() && line.startsWith(this.id))
.collect(Collectors.toList());
}
}
No issue is raised when the qualifier is required to disambiguate. Inside an anonymous class or a named inner class, plain this refers
to the instance of that inner class, so the qualified form is the only way to reach the enclosing instance:
public class Invoice {
private String id;
public Runnable renamer(String newId) {
return new Runnable() {
@Override
public void run() {
Invoice.this.id = newId; // Compliant: "this" would be the Runnable
}
};
}
}
Removing the qualifier is therefore safe only when the enclosing class is the innermost class around the expression. If an issue is raised on a
qualified this that does resolve to a different object than a plain this would, the finding is a false positive and worth
reporting rather than silencing.