reset password
Author Message
kknaur
Posts: 540
Posted 17:33 Apr 10, 2014 |

Be sure to follow good coding practice and use the this keyword when you are working with classes.  It may seem tedious, but it can help to prevent some strange logic errors, especially if you have local variables/method parameters which are the same names as your class member variables.

For example if my class was:

class A {
  int x;
  A() {
    some code
  }

  public int calc() {
    return some code
  }
}

Then anywhere in my class if I were referring to member variable x, i would say this.x.  You can also call class methods using this such as this.calc().  Some schools of thought believe that you should use this to explicitly refer to any method or member variable of a class.  For me, I really only care when you are referring to member variables.  Please get in the habit of qualifying all of your class member variables with this.variableName.  Again it can help to reduce some strange bugs in your code.

Last edited by kknaur at 17:34 Apr 10, 2014.