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

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows