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

Python script to show Laptop Battery Percentage

Source:  https://www.geeksforgeeks.org/python-script-to-show-laptop-battery-percentage/ # python script showing battery details  import psut...