What is the significance of a copy constructor in Java?

  • It creates a new object with the same state as an existing object
  • It creates a shallow copy of an object
  • It creates a deep copy of an object
  • It allows a class to be copied directly without instantiation
In Java, a copy constructor is a constructor that takes an object of the same class as a parameter and creates a new object with the same state as the parameter object. It's used to clone objects. Option 2 creates a copy that shares references (shallow copy), and option 3 creates a new object with copies of all referenced objects (deep copy). Option 4 is not a typical use of copy constructors.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *