this Keyword in Java

this holds the reference of the current object. See underneath

example:

class demo
{
                int  x=10;
                void show(int x,demo d1)
                {
                                System.out.println(this.x);
                                System.out.println(d1.x);
                                System.out.println(x);
                }
                public static void
main(String…s)
                {
                                demo d1=new
demo();
                                d1.show(20,d1);
                }
}

Output

10
10
20
  • In Java as a matter, of course, this is passed to all non-static techniques. this can’t be utilized into static strategies.
  • this is the reference variable or nearby variable, it goes to stack.
  • At the point when nearby and worldwide variables are same then this circumstance is known as information shadowing. In such circumstance, this keyword is utilised to recognize nearby and worldwide variable.

Consider underneath example:

class demo
{
                int x=10;
                void show()
                {
                                int x=20;
                                System.out.println(this.x);           //golabal variable
                                System.out.println(x);                   //local variable, priority goes to local variable
                }             
                public static void
main(String…s)
                {
                                demo d=new
demo();
                                d.show();
                }
}

Output

10
20

Watch beneath video instructional exercise to comprehend this watchword effectively:

Leave a Comment

error: Alert: Content is protected!!