Monday, February 26, 2018

Java program : Use of 'this' keyword

class ThisConversion
{
    int x;        //These are INSTANCE variables
    String name;
    ThisConversion(int x, String name)  //here x and name are LOCAL variables of the Ctor
    {
        this.x=x;            //we use this keyword to convert LOCAL variables to INSTANCE variables
        this.name=name;        // If LOCAL and INSTANCE variables have SAME name, then we use 'this' keyword
    }
    void disp()
    {
        System.out.println(name +"  "+x);
    }
    public static void main(String[] args)
    {
        new ThisConversion(3,"yash ashutosh").disp();   //o/p yash3
       
    }
}

Thanks
Happy Computing!

No comments:

Post a Comment

Sacred Thought

26 April 2024  Dear friends, I write the explanation of two verses of Geets for all of you, I hope you all will like it and benefit from it....