Classes >
this is one of the java keywords and it helps in referring to the current object. When we use the dot(. ) operator on this keyword, then we can access the member variables of the current object. this keyword can also be used to call one constructor from another constructors of the same class.
The parameter names passed to the constructors can have the same name as the member variables of that class.
For e.g., the constructor with parameters nameParam , marksParam and sectionParam
class Student Student(String name, int marks, char section) this keyword to distinguish the member variable name from the parameter name . When this.name is called it is the member variable, and when only name is called it is the parameter. Similarly, this.marks and this.section are member variables, where as marks and section are parameters.
The other use this keyword is to call the same class constructors. For e.g., the following code // CONSTRUCTOR 1 // CONSTRUCTOR 1 this keyword, both to distinguish (or identify) the member variables from the parameters and also to call the same class constructor.
|