10 This Keyword in Java
10 This Keyword in Java
ANS:
➢ In Java, this keyword is a reference to the current object within an instance
method or a constructor.
➢ It can be used to refer to the current instance of the class and access its
members, including instance variables and methods.
➢ This keyword can also be used to differentiate between instance variables
and parameters or local variables with the same name.
(1) Using the ‘this’ keyword to refer to current class instance variables.
(2) Using this() to invoke the current class constructor
(3) Using ‘this’ keyword to return the current class instance
(4) Using ‘this’ keyword as the method parameter
(5) Using ‘this’ keyword to invoke the current class method
(6) Using ‘this’ keyword as an argument in the constructor call
Lets understand each way to use this keyword.
(1) Using the ‘this’ keyword to refer to current class instance variables
➢ Example:
class Rectangle
{
double length;
double breadth;
double calculateArea()
{
return length*breadth;
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
System.out.println("Area of Rectangle: " +(myrec.calculateArea()));
}
}
➢ This can be helpful when you have multiple constructors in a class with
different parameters, and you want to avoid duplicating initialization
code.
class Rectangle
{
double length;
double breadth;
Rectangle()
{
System.out.println("Non-argument constructor called!");
}
double calculateArea()
{
return length*breadth;
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
System.out.println("Area of Rectangle: " +
(myrec.calculateArea()));
}
}
(3) Using ‘this’ keyword to return the current class instance
➢ Using the 'this' keyword to return the current class instance refers to a
programming technique in object-oriented languages where 'this' is used
within a method to explicitly return the instance of the class on which the
method is invoked.
➢ This allows for chaining method calls on the same instance, which can
enhance code readability and conciseness.
➢ By returning 'this', the method indicates that it has completed its operation
and returns the current instance, enabling method chaining without needing
to explicitly store and return the instance manually.
class Rectangle
{
double length;
double breadth;
Rectangle(double l, double b)
{
length = l;
breadth = b;
}
Rectangle meth()
{
Rectangle r = new Rectangle(length*10, breadth*10);
return this;
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
Rectangle ob;
System.out.println("The original length and breadth: "+myrec.length +"
and " +myrec.breadth);
ob = myrec.meth();
System.out.println("The modified length and breadth: "+ob.length +"
and " +ob.breadth);
}
}
(4) Using ‘this’ keyword as the method parameter
➢ Using the 'this' keyword as the method parameter is a concept in object-
oriented programming where 'this' is passed as an argument to a method
within the same class.
➢ This allows the method to access or modify the instance variables and
methods of the current object.
class Rectangle
{
double length;
double breadth;
Rectangle(double l, double b)
{
length = l;
breadth = b;
}
void callmethod()
{
calculateArea(this);
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
myrec.callmethod();
Rectangle myrec2 = new Rectangle(7,4);
myrec2.callmethod();
}
}
(5) Using ‘this’ keyword to invoke the current class method
➢ Using the 'this' keyword to invoke the current class method is a technique
in object-oriented programming where 'this' is employed within a method
to call another method within the same class.
➢ It ensures that the method being called is explicitly from the current
instance, enhancing code readability and reducing potential confusion.
class Rectangle
{
double length;
double breadth;
void calculateArea()
{
double area = length*breadth;
System.out.println("Area of the Rectangle: " +area);
}
void callmethod()
{
this.calculateArea(); //calls current class method
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
myrec.calculateArea();
Rectangle myrec2 = new Rectangle(7,4);
myrec2.callmethod();
}
}
class Rectangle
{
double length;
double breadth;
Rectangle()
{
Rectangle r = new Rectangle(this);
Rectangle(Rectangle rt)
{
rt.length = 10; //using this to point out above variables
rt.breadth = 12;
}
double calculateArea()
{
return length*breadth;
}
}
class this_keyword
{
public static void main(String[] args) {
double area;
Rectangle myrec = new Rectangle();
System.out.println("Area of Rectangle: " +(myrec.calculateArea()));
}
}