Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#148 references to non-static fields should be quailified

release_3.4
closed
Check (274)
5
2012-10-10
2003-03-03
Peter Dobratz
No

Check to ensure that all field references are prefixed
by either "this." or "super." as appropriate.

Rational: This allows readers to immediately recognize
a field reference as opposed to a local variable. It
also allows the compiler to do more type checking and
avoids the problem of shadowing.

--Peter

Discussion

  • Tim Tyler
    Tim Tyler
    2003-06-14

    Logged In: YES
    user_id=796025

    Only "this" can be done locally.

    Foo foo;
    void setFoo(Foo foo) {
    this.foo = foo;
    }

    ...is a somewhat problematic idiom in this context.

    Local variables hiding fields might be best dealt
    with by suppressing the check in that case.

     
  • Logged In: YES
    user_id=863158

    I would like to add my support for this RFE.
    I far prefer using this to clearly indicate that I'm using a local field rather
    than forced naming conventions like _blah or whatever. Using this is
    also more consistent with what you have to write when accessing fields
    of enclosing types, ie Outer.this.blah

    I think that forcing qualification of just local members with this. is still
    useful given that it is usually considered undesirable to have protected
    fields.

    Used in conjunction with the forcing of final parameters and non-public,
    non-protected field declarations, this check would be quite effective.

     
  • Logged In: YES
    user_id=746148

    See also 755550 (require explicit "this.")

     
  • Logged In: YES
    user_id=746148

    The check committed to CVS for 3.4
    module RequireCheck.