mercredi 27 mai 2015

Where to use "this" in java?

As far as i know this is used in following situations:

  1. this keyword is used when we want to refer to instance variable having same name as local variable.
  2. Calling one constructor to other in same class.
  3. Pass the class instance as an argument to the method.
  4. Accessing the outer class variables.

But I have gone through my project code where they are using this in getters like:

class a{
int time;

int getValue(){
return this.time*5.;
}
}

As far as i know,every object has its own copy of instance variables and methods,so will returning this way makes any sense. Please clarify.

Stackoverfow problem referred:When should I use "this" in a class?

Aucun commentaire:

Enregistrer un commentaire