on Leave a Comment

This Keyword in Java With Examples

The java this keyword is used to refer the current object. There are many uses of java this keyword.

Uses of java this keyword

this keyword can be used to refer instances of current object.

this keyword can be used to refer methods of current object.

this keyword can be used to refer constructors of current object.

this keyword can be passed as  argument to method of current object.

this keyword can be passed as argument to constructor of current object.

this keyword can be used to return current object.

this keyword can be used to refer instances of current object

If the local variables and instance variables are same, this is ambiguity for JVM, To solve this ambiguity, java this keyword is used.

Example:

class Area
{
 float l;
 float b;
 Area(float l, float b)
 {
  this.l = l;
  this.b = b;
 }
 void show()
 {
  System.out.println("Length = "+l+"\nBreadth = "+b);
 }
}
class ThisKeyword
{
 public static void main(String[] args) {
  Area obj = new Area(10, 8);
  obj.show();
 }
}

Output:

Length = 10.0
Breadth = 8.0

this keyword can be used to refer methods of current object

We can also invoke methods using this keyword. But we can also call methods by name. If we don't use this keyword for invoking methods, then compiler automatically adds this keyword.

Example:

class Area
{
 float l;
 float b;
 Area(float l, float b)
 {
  this.l = l;
  this.b = b;
 }
 void show()
 {
  System.out.println("Length = "+l+"\nBreadth = "+b);
  System.out.println("\nArea = "+this.calculate());
 }
    float calculate()
 {
  return this.l*this.b;
 }
}
class ThisKeyword
{
 public static void main(String[] args) {
  Area obj = new Area(10, 8);
  obj.show();
 }
}

Output:

Length = 10.0
Breadth = 8.0

Area = 80.0

this keyword can be used to refer constructors of current object

We can also refer constructors of class using this keyword. Java this can be used to invoke both parameterized and non-parameterized constructor.

Example:

class Area
{
 float l;
 float b;
 Area()
 {
  System.out.println("Calculates area of rectangle");
 }
 Area(float l, float b)
 {
  this();
  this.l = l;
  this.b = b;
 }
 void show()
 {
  System.out.println("Length = "+l+"\nBreadth = "+b);
  System.out.println("\nArea = "+this.calculate());
 }
    float calculate()
 {
  return this.l*this.b;
 }
}
class ThisKeyword
{
 public static void main(String[] args) {
  Area obj = new Area(10, 8);
  obj.show();
 }
}

Output:

Calculates area of rectangle
Length = 10.0
Breadth = 8.0

Area = 80.0

this keyword can be passed as  argument to method of current object

We can pass this keyword as argument to method of current object.

Example:

class Area
{
 float l;
 float b;
 Area(float l, float b)
 {
  this.l = l;
  this.b = b;
  show(this);
 }
 void show(Area ob)
 {
  System.out.println("Length = "+l+"\nBreadth = "+b);
 }
}
class ThisKeyword
{
 public static void main(String[] args) {
  Area obj = new Area(10, 8);
 }
}

Output:

Length = 10.0
Breadth = 8.0

this keyword can be passed as argument to constructor of current object

We can pass this keyword as argument to constructor of current object.

Example:

class Area
{
 float l;
 float b;
 Area()
 {
  ThisKeyword t = new ThisKeyword(this);
 }
}
class ThisKeyword
{
 ThisKeyword(Area o)
 {
  System.out.println("Constructor is invoked");
 }
 public static void main(String[] args) {
  Area obj = new Area();
 }
}

Output:

Constructor is invoked

this keyword can be used to return current object

We can return an object using this keyword.

Example:

class Area
{
 float l;
 float b;
 Area call()
 {
   return this;
 }
 void show()
 {
  System.out.println("Length = "+l+"\nBreadth = "+b);
 }
 Area(float l, float b)
 {
  this.l = l;
  this.b = b;
 }
}
class ThisKeyword
{
 ThisKeyword(Area o)
 {
  System.out.println("Constructor is invoked");
 }
 public static void main(String[] args) {
  new Area(4, 5).call().show();
 }
}

Output:

Length = 4.0
Breadth = 5.0

0 comments:

Post a Comment