Pages

Saturday, 4 January 2020

Program on this keyword

//Program with out this keyword

 class Test
 {
   int a,b;
   void setData(int a,int b)
    {
      a=a;
      b=b;
    }
   void showData()
    {
     System.out.print("value of a=" +a);
     System.out.println();
     System.out.print("value of b=" +b);
   }

 public static void main(String[] args)
  {
    Test obj=new Test();
    obj.setData(10,20);
    obj.showData();
  }
}

output:


//Program with this keyword


class Test
 {
   int a,b;
   void setData(int a,int b)
    {
      this.a=a;
      this.b=b;
    }
   void showData()
    {
     System.out.print("value of a=" +a);
     System.out.println();
     System.out.print("value of b=" +b);
   }


 public static void main(String[] args)
  {
    Test obj=new Test();
    obj.setData(10,20);
    obj.showData();
  }
}

output:


No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...