|
Hi there, let me give you an example of "this" that will help you grasp this concept better. Assume you have a class, called Dog. The Dog has three variables, age, height and weight. And you have a constructor:
public class Dog
{
int age;
int height;
int weight;
public Dog(int age, int height, int weight)
//Constructor. The calling method will parse int age, height and weight
{
/*
So this is the problem. both variables have the same name! age that is passed in and age that is the class variable! so to solve it, you use "this"*/
this.age=age;
this.height=height;
this.weight=weight;
}
}
So in the three lines of "this.age" "this.height" and "this.weight". it sets the CLASS variable, the one declared in the lines right after the class declaration to the three integers that are passed in.
|