this keyword in java - , this reference in Java

this keyword in java

‘this’ is a reference variable that refers to the current object. It is a keyword in java language which represents a current object.

The ‘this’ keyword in java solve the confusion when there are the same instance variable and local variable.

With the help of this keyword, we can access current class methods or function, instance variable, and constructors of the same class within the class.

The ‘this’ can only be used inside the class and cannot be used outside the class. The ‘this’ reference is a constant reference.

Usage of ‘this’ reference is as follows:

  • ‘this’ keyword is used to refer to the current instance variable
  • ‘this’ implicitly reference used by the system
  • ‘this’ used as a method argument
  • ‘this’ invoke a constructor 
  • ‘this’ returns the current class instance
  • ‘this’ used as an argument in the constructor call

'this' Keyword in Java with Example

this’ keyword is used to refer to the current instance variable

 public class Summation

{

    int a, b, c;

    void sum(int a, int b)

    {

        a=a;

        b=b;      

    }

    void display()

    {

        System.out.println("a=" + a);

        System.out.println("b=" + b);

    }

    public static void main(String args[])

    {

        Summation ob=new Summation();

        ob.sum(30, 40);

        ob.display();

    }

}

Output

a=0

b=0

Reason: In the above method sum, the arguments are declared as a and b, and the instance variables are also named as a and b.

void sum(int a, int b)

 {

      a=a;

      b=b;      

 }

During compilation compiler get confused and fail to differentiate between instance variable and local variable (argument) , so it assign 0 to a and b

Solution to this confusion is java this keyword.

public class Summation

{

    int a, b, c;

    void sum(int a, int b)

    {

        this.a=a;                        // this keyword used

        this.b=b;           // this keyword used

    }

    void display()

    {

        System.out.println("a="+a);

        System.out.println("b="+b);

    }

    public static void main(String args[])

    {

        Summation ob=new Summation();

        ob.sum(30,40);

        ob.display();

    }

}

After providing ‘this’ this.a=a and this.b=b

Compiler become aware that the left side is the instance variable and right side local variable (formal argument) and the output as

a=30

b=40


‘this’ used as a method argument

this can be passed as an argument to a method in a class. It is used to represent an object of that class.

It will clear with the following example

class MethodArgument

{

  void firstMethod(MethodArgument obj) // Object as an argument

  {

    System.out.println ("This is method call");

  }

  void secondMethod()

  {

    firstMethod (this);

  }

}

In the above class object passed as an argument. And in the following class called with the help of MethodArgument class

public class MethodCall

{

   public static void main(String args[])

   {

       MethodArgument ob=new MethodArgument();

       ob.secondMethod();

    }

}


‘this’ invoke a constructor

 The this() keyword can be used to invoke the current class constructor. It is used as constructor chaining.

public class Constructor

{

    Constructor()

    {

        System.out.println("I am inside Constructor class");

    }

    Constructor(int x)

    {

        this();

        System.out.println(x);

    }

}

In the above class a default constructor called within a parameterized constructor with the help of this.

Note: It should be kept in mind that this should be the first statement in the other constructor.

public class CallConstructor

{

    public static void main(String args[])

    {

        Constructor ob=new Constructor(200);       

    }

}


this can be used to return the current class instance

public class AreaSquare

{

    int s, area;

    AreaSquare(int side)

    {

        s = side;

       area = s*s;

    }

    void areaSquare()

    {

       area = s*s;

       System.out.println("Area of Square:" + area);

       System.out.println("Calling method");

    }

    AreaSquare getObj()

    {

        return this;

    }

}

In the above program, this is used return. The return must be class other return will not work. For running or calling the areaSquare() method we have to take the reference as

new AreaSquare(10).getObj().areaSquare();       

public class CallAreaSquare

{

    public static void main(String args[])

    {       

        System.out.println("Calling method");

        new AreaSquare(10).getObj().areaSquare();       

    }

}

Output

Calling method

Area of Square:100

Inside method

More Java program







SHARE THIS
Previous Post
Next Post