The RequireThis rule has the correct use, since it can prevent a possible error in the methods and constructors when it is applied to fields. The code below is almost certainly an error:
void setSomething(String something) { something = something; }
Code like this will compile, but will do nothing but override the value of the method parameter to itself. Most likely, the author intended to do this:
void setSomething(String something) { this.something = something; }
This is a typo that can happen and deserves to be checked, because it can help prevent debugging problems if the code failed because this.something not installed much later in the program.
The checkstyle settings allow you to save this useful check for fields, at the same time omitting the largely unnecessary method check, setting up the rule as follows:
<module name="RequireThis"> <property name="checkMethods" value="false"/> </module>
When it comes to methods, this rule has no real effect, because calling this.getMeSomething() or just getMeSomething() does not affect the resolution of the Java method. The call to this.getSomethingStatic() still works when the method is static, it is not an error, it is only a warning in various IDEs and static analysis.
Tendayi mawushe
source share