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.
[ 0 ] Comments