This keyword | This keyword in java | Use of this keyword in java

In this article this keyword we give the information about this keyword in java it is a reference variable that is used to refer to the current object in a method or constructor.

this keyword:-

This keyword is a reference variable that is used to refer to the current object in a method or constructor.

When both the instance variable and local variable are same in the program. That’s why when the value of the instance variable(left) is assigned to the local variable (right), then the compiler does not understand which variable; instance and which variable is local. That’s why we use this keyword so that the compiler can easily understand which is the instance variable and which is the local variable.

Advantages this keyword:-

  • this keyword is used to refer to the instance variable of the current class.
  • It is used to invoke the method of the current class.
  • It is used to invoke the current class constructor.
  • advantages of this can be passed as an argument in the method call.
  • this keyword can be passed as an argument in the constructor call.
  • this keyword is used to return the current class instance from the method.

When we keep both Instance variable and Local variable same in the program.

Program:-

class Sample

{

            int n1;                         // instance variable

            int n2;                         // instance variable

            Sample(int n1, int n2)

{

// a and b is local variable

                        n1 = n1;

                        n2 = n2;

}

            void show()

{

                        System.out.println(“Value of n1 : ” + n1);

                        System.out.println(“Value of n2 : ” + n2);

}

            public static void main(String args[])

{

                        Sample s = new Sample(15, 16);

                        s.show();

}

}

OUTPUT :

Value of a : 0

Value of b : 0

Instance variable and local variable with this keyword in the program, these two variables have been separated.

class Sample

{

            int n1;      // instance variable

            int n2;      // instance variable

            Sample(int n1, int n2)

{

// a and b is local variable

                        // using this keyword

                        this.n1 = n1;              //Instance Variable = Local Variable;

                        this.n2 = n2;              //Instance Variable = Local Variable;

}

            void show()

{

                        System.out.println(“Value of n1 : ” + n1);

                        System.out.println(“Value of n2 : ” + n2);

}

            public static void main(String args[])

{

                        Sample s = new Sample(15, 16);

                        s.show();

}

}

OUTPUT :

Value of a : 15

Value of b : 16

1:- To refer to the instance variable of the current class

class Area

{

int l;

int b;

// Parameterized constructor

Area(int l, int b)

{

this.l = l;

this.b = b;

}

void display()

{

//Displaying value of variables l and b

System.out.println(“Area of Rectangle = ” + l*b);

}

public static void main(String[] args)

{

Area obj = new Area(10, 4);

obj.display();

}

}

O/P:-
Area of Rectangle = 40

2:- To invoke the method of current class

example:-

class Area

{

void rect()

{

int a=10, b=5;

System.out.println(“Area of Rectangle=”+a*b);

}

void square()

{

int s=5;

System.out.println(“Area of Square=”+s*s);

this.m();

}

}

class TestThis4

{

public static void main(String args[])

{

Area p=new Area();

p.square();

}

}

O/P:-
Area of Square=25

Area of Rectangle=50

3:- To invoke the current class constructor

Its example:-

class Test

{

int x;     int y;    

//Default constructor

Test()

{

this(100, 200);

System.out.println(“Default Constructor”);

}     

//Parameterized constructor

Test(int x, int y)

{

this.x = x;

this.y = y;

System.out.println(“Parameterized Constructor”);

System.out.println(“Addition of X & Y is =”+(x+y));

}

public static void main(String[] args)

{

Test object = new Test();

}

}

O/P:-
Parameterized Constructor

Addition of X & Y is 300
Default Constructor

4:- As argument in method call

Its example:-

class first

{

void fun1(first obj)

{

System.out.println(“Have a nice Day!…”);

}    void fun2(){    fun1(this);

}

public static void main(String args[])

{

first obj = new first();

obj.fun2();

}

}

O/P:-
Have a nice Day!…

Related Link:

Some More: DBMS/ WT/ DMDW

Santosh Nalawade

Work as Assistant Professor and Web Developer.

Leave a Reply

Your email address will not be published. Required fields are marked *