Object reference or Copy Constructor using Java



A class that
contains a method which will have parameter of type class itself (object
reference), How to pass value to that method from main.





Program:





class Rectangle


 {


  double l,b;





  Rectangle(double x,double y)


   {


    l=x;


    b=y;


   }


  double RecArea(Rectangle ob)


    {


     return (ob.l*ob.b);


    }


 }


class Sk20


 {


 


  public static void main(String args[ ])


   {


     Rectangle 
ob1= new Rectangle(4,5);


     double ar = ob1.RecArea(ob1);


     System.out.println(+ar);


    }


 }





Compile:


D:\>javac
Sk20.java





Run:


D:\>java Sk20





Output:




20.0


0 Comments