ICSE Class 10 Computer Applications
Question 7 of 19
Constructors — Question 7
Back to all questions 7
Question Question 7
What is the use of the keyword this?
Within a constructor or a method, this is a reference to the current object — the object whose constructor or method is being invoked. The keyword this stores the address of the currently-calling object.
For example, the below program makes use of the this keyword to resolve the conflict between method parameters and instance variables.
class Area
{
int length, breadth;
public Area(int length, int breadth) {
this.length = length;
this.breadth = breadth;
}
public void Display() {
int area = length * breadth;
System.out.println("Area is " + area);
}
public static void main(String args[J) {
Area area = new Area(2, 3);
area.Display();
}
}