Write a program in java to demonstrate 3rd use of “this” keyword?

class This3

{

                int a,b;

                public This3(int x,int y)

                {

                                a=x;

                                b=y;

                                //"return this;" by JRE

                }

                public This3 swap()

                {

                                int c=a;

                                a=b;

                                b=c;

                                return this;

                }

                public This3 display()

                {

                                System.out.println(a);

                                System.out.println(b);

                                return this;

                }

                public static void main(String arg[])

                {

                                new This3(5,7).display().swap().display();

                }

}

3rd use of this keyword.PNG

, , , , , ,

Leave a Reply