What is copy constructor in Java?

: 533
Paper : Java FAQ (Frequently Asked Questions and answers) for freshers | Platform : Object-oriented programming Language | Category : Programming FAQs

Java does not support a copy constructor like C++. Moreover, there are some concepts that work like copy constructor in java. We can copy the value of one object to another object. The ways you can do:

  • Through constructor

Example: We have a class of employees.

employees(int i, String name)

{

this.i =i;

this.name=name;

}

  • Assigning the values of one object to another.

Example: employee(employee e)

{

i = e.i;

name = e.name;

}