Introduction to this keyword

Introduction to this keyword


Posted in : Core Java Posted on : October 9, 2010 at 4:54 PM Comments : [ 0 ]

This section contains the detail about the this keyword in java.

Introduction to this keyword

Sometimes method will need to refer to the object that invoked it. For this we have 'this' keyword in Java. '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 .

For Example :

public class Box {
    int length;
    int width;
    int height;
    public Box(int length, int width, int height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }
}

In the above example, "this.length" refers to the 'length' variable which invoke it. The keyword 'this' also used to resolve any name space collision that might occur between instance variable and local variable.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics