Copy a Constructor in Java


☞This constructor is used to copy the initial values of the instance variables of an object to the instance variables of another object.

Example

class Area{
    
    int length;
    int breadth;

    Area(int l, int b){
        length=l;
        breadth=b;
    }
    
    Area(Area obj){
        length=obj.length;
        breadth=obj.breadth;
    }
    
    void display(){
        System.out.println(length*breadth);
    }
    
    public static void main(String args[]){
    
        Area rectangle1 = new Area(4,5);
        Area rectangle2 = new Area(rectangle1);
        rectangle1.display();
        rectangle2.display();
        
    }
    
}

Output

20
20