Uses Of this keyword in java | Polymorphism-Basic-IT-Topic

Uses Of this keyword in java

 this keyword in java

Working with “this” keyword:-

This keyword in java is a keyword which always refers to a current class object.T he difference between ‘new’ and ‘this’ is new is responsible to call a  constructor and create a new object. Whereas “this” is responsible to call the constructor without allocating memory. this keyword can be very useful in the handling of Variable Hiding.

By the use of this keyword, a programmer can refer to any member of the current object within an instance method or a constructor.

this can be used by itself to simply refer to member variables and methods

The following are the uses of “this” keyword in Java:-

Uses-1

To call a constructor from another constructor within a single class without allocating new memory, we can use this keyword .” this” should be the first statement in a constructor.

Example

class Demo

{

        Demo()

        {

                        System.out.println("i am in demo");

        }

}

class Test extends Demo

{

        Test()

        {

                        this(123);

                        System.out.println("i am in default");

        }

        Test(String x)

        {

                        this();

                        System.out.println("i am in string");

        }

        Test(int x)                                                                                                                                       

        {

                        this(12.3);

                        System.out.println("i am in integer ");

        }

        Test(double x)

        {

                        System.out.println("i am in double");

        }

        public static void main(String[] args)

        {

                                        new Test("hello");

        }

}

Output

i am in demo

i am in double

i am in integer

i am in default

i am in string

 

Uses-2

When the local and instance variable name is the same, ‘this’ keyword is used to create a difference between them.

Example

class Test

{

        int x=20;




        void show()

        {

                        int x=10;

                        System.out.println(x);

                        System.out.println(this.x);

        }

        public static void main(String[] args)

        {

                                        new Test().show();

        }

}

Output

10

20

 

Uses-3

This keyword can be passed as an argument within the constructor.

Example

class Demo

 {

        int x=10;

        Demo( Test t)

        {

                        int x=30;

                        System.out.println(x);

                        System.out.println(this.x);

                        System.out.println(t.x);

        }

 }

 class Test

 {

        int x=20;

        void display()

        {

                        new Demo(this);

        }

        public static void main(String[] args)

        {

                        new Test().display();      

        }

 }

Output

30

10

20

Uses-4

‘this’ keyword is used to display the current class object hash code.

Example

class Test

{

        int a=10;

        void show()

        {

                        System.out.println(this);

        }

        public static void main(String[] args)

        {

                        new Test().show();

        }

}

Output

[email protected]


Uses-5

“this” keyword can be used as a return value.

Example

class Demo

{

        int x=10;

        void display(Test obj,int x)

        {

                        System.out.println(x);

                        System.out.println(this.x);

                        System.out.println(obj.x);

        }

}

class Test

{

       

int x=20;

        void show()

        {

                        int x=40;

                        new Demo().display(this,this.x);

        }

        public static void main(String[] args)

        {

                        new Test().show();

        }

}

Uses-6

“this” keyword can be passed as an argument within a method.

Example

class Test

{

        int x=20;

        void show()

        {

                        int x=40;

                        new Demo().display(this,this.x);

        }

        public static void main(String[] args)

        {

                        new Test().show();

        }

}

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community