Friday, 31 May 2013

this keyword in JAVA

‘this’ is used for pointing the current class instance. It can be used with variables or methods. 

'this' can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked.

The keyword 'this' in Java refers to the current class instance. For example, if a class defines a method named Calculate, you can call that method from another method within the same class like this:

    this.Calculate();

Of course, you can also call the Calculate method without the this keyword:

    Calculate();

Thus, in most cases, the keyword this is not necessary.

However, sometimes the this keyword can come in handy.

For example:

The 'this' keyword helps us to avoid name conflicts. As we can see in the program that we have declare the name of instance variable and local variables same. Now to avoid the confliction between them we use this keyword.

public class Employee
{
    string lastName;
    string firstName;
    public Actor(String lastName, String firstName)
    {
        this.lastName = lastName;
        this.firstName = firstName;
    }
}

The 'this' keyword is required to distinguish among the parameters named lastName and firstName and the instance.

Sometimes, you use the this keyword by itself to pass a reference to the current object as a method parameter. 

You can print the current object to the console by using the following statement:

System.out.println(this);

Note: Since this is instance of a class it cannot be used inside a static method.

'this' keyword refers to the object that is currently executing. It is useful for a method to reference instance variables relative to the this keyword.

Syntax of using 'this' keyword:

this.varName

varName is a name of an instance variable.

Another use of the this keyword is it allows one constructor to explicitly invoke another constructor in the same class.

Syntax to call another constructor of same class using this keyword  

this(args);

Usage of this keyword:
  • This keyword can be used to refer current class instance variable.
  • This() can be used to invoke current class constructor and used to call overloaded constructor.
  • this can be used to invoke current class method.
  • this can be passed as an argument in the method call.
  • this can be passed as argument in constructor call.
  • this can also be used to return the current class instance.
  • "this" keyword can not be used in static context i.e. inside static methods or static initializer block.
Example:

*******************************************************

public class ThisKeywordExample {

int x = 0;
int y = 0;
String name = null;

public ThisKeywordExample() {
System.out.println("constructor");
}

ThisKeywordExample(int x) {
this(10, 20);// called overloaded constructor of this class
this.x = x;
System.out.println("Value of x is " + this.x);
}

ThisKeywordExample(int x, int y) {
this();// called default constructor of this class
this.x = x;
this.y = y;
// In following lines of code this.x represents the instance variable x // where as x represents local variable x. System.out.println("Value of x and y are " + this.x + " " + this.y); System.out.println("Value of x and y are " + x + " " + y);
}

public void setName(String name) {
        // to avoid name conflicts
this.name = name;
}

public void getName() {
// to invoke current class setName() method.
this.setName("JAVA");
System.out.println("name of employee is " + name);
}

public void getClassRef(ThisKeywordExample example) {
System.out.println("this shows how 'this' passed as parameter in  
                                    method call ");
}

public ThisKeywordExample getClassType() {
  //this can be passed as an argument in the method call.
  this.getClassRef(this);
  // this can also be used to return the current class instance.
  return this;
}

public static void main(String a[]) {
ThisKeywordExample example = new ThisKeywordExample(10);
example.getName();
          //getClassType() returns current class object.
         ThisKeywordExample example2 = example.getClassType();
}
}

*******************************************************

No comments:

Post a Comment