Site hosted by Angelfire.com: Build your free website today!
 

this

The keyword this has several uses in Java.

  • Refers to the current object.

  • Refers to a constructor of the current class.

  • Helps to avoid namespace conflicts between a method's parameter list and its variables.

class Shape {
   int x, y;
   Shape() {
     this(0, 0);  // call another constructor
                  // must be the 1st statment in the constructor
   }
   Shape(int x, int y) {
     this.x = x;   // avoid name conflict
     this.y = y;
   }
   . . .
}