'this' pointer is used when a method has to refer to an object that has invoked it. 'this' pointer can be used inside any method to refer to the current object. This means that this is always used as a reference to the object on which the method was invoked. We can use this anywhere as reference to an object of the current class type is permitted. Since, it is illegal in Java to declare two local variables with the same name inside the same or enclosing scopes; we can have local variables, which include formal parameters to methods, which may overlap with the names of the class instance variables. However, when a local variable has the same name as an instance variable, the local variable hides the instance variable.
emp_id and emp_salary have been used as the names of the parameters tothe Employee() constructor inside the Employee class. If they had been, then emp_id would have referred to a formal parameter, hiding the instance variable emp_id. While it is usually easier to simply use different names, there is another way around this situation. Because this lets you refer directly to the object, you can use it to resolve any name space collisions that might occur between instance variables and local variables. For example, here is another version of Employee( ), which uses emp_id and emp_salary for parameter names and then uses this to access the instance variables by the same name:
employee(double emp_id, double emp_salary)
this.emp_id = emp_id;
The use of this in such a context can sometimes be confusing, and some programmers are careful not to use local variables and formal parameter names that hide instance variables. But other programmers believe that it is a good convention to use the same names for clarity, and use this to overcome the instance variable hiding. It is a matter of taste which approach you adopt. Although this is of no significant value in the examples just shown, it is very useful in certain situations . This is illustrated in the following examples:
t = this.x; // the x instance variable for this object
this.myMethod(this); // calls the myMethod method, defined in this class and passes it to the current object
return this; // returns the current object
Program illustrates the use of the keyword this. In the program, the main() method creates an object ob of class CircleEx. In the method CircleEx, this.x refers to the first passed value, that is, 10. Similarly this.y and this.radius refers to the second and third values passed to the method CircleEx, respectively (20 and 10). The method display in this program displays the values of x, y and radius when this.display() is invoked from the method CircleEx .
Illustrating the use of the keyword this.
public dass CirdeEx
public CircleEx(int x, int y, int radius)
this.x = x;
this.radius = radius;
System.out.println("value of x is" +x);
System.out.println("value of x is" +y);
System.out.println("value of x is" +radius);
public static void main(String arg[ ])
System.out.println("Use of keyword this");
CircleEx ob = new CirdeEx(1 O. 20, 10);
The output of Program is as shown below:
Use of keyword this
value of x is 10
value of x is 20
value of x is 10
In most programs, this can be omitted entirely. Programmers can refer to both instance variables and method calls defined in the current class simply by invoking their name, this is implicit in those references. This is shown below:
t = x // the x instance variable for this object
myMethod(this) // call the myMethod method. Defined in this class
The keyword this is a reference to the current instance of a class. It should be used only within the body of an instance method definition. Class methods (methods declared with the keyword static) cannot use this.