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

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...