Note: I'm aware of this question: Why doesn't Java have a copy constructor?. This question is slightly diffrent.
I'm know that the following snippet only creates a new reference to an object existing somewhere:
MyClass obj = new MyClass();
MyClass copy = obj;
But why Java wouldn't provide a default copy-constructor in the following case:
MyClass obj = new MyClass();
MyClass copy = new MyClass(obj);
I believe the implicit copy construtor could do something simillar to C++, i.e. call copy constructor on each of the members. This would result in a deep-copy of the object, presuming that all classes which manage resources have correctly implemented their copy contructors.
Adding such feature should be backward compatible also, because now the second snippet wouldn't compile without explicit copy contructor for the class.
So, to make my question more precise:
- Is there anything preventing Java from creating implicit copy constructor, which would call copy constructor on all members?
- Would adding implicit copy constructor now possibly break any existing program?