Results 1 to 5 of 5
- 05-25-2011, 03:50 AM #1
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
- 05-25-2011, 04:37 AM #2
I don't think Java has a copy constructor. Copies are made by using the Object class's clone method.
-
And regardless, you can't override a constructor since they are not "virtual" while non-static methods are.
- 05-25-2011, 09:41 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
It does have the concept, since it is after all simply a constructor that takes an object of the same class as a parameter and copies the data.
To the OP, you would need to copy the child objects as well.
This means they would need to be either Cloneable or themselves have valid copy constructors.
This is the difference between a shallow copy (which simply copies the values as is) and a deep copy.
Though I would suggest you really think through what you're doing and ensure you do need to be copying.
- 05-25-2011, 12:27 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
The fact 'private' counts on a class basis; you can do this:
It's up to you how to copy the private object u.Java Code:public class T { private U yourPrivateObject; public T(T t) { // copy ctor this.yourPrivateObject= // copy of t.privateObject; } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
how can i deep copy objects themselves instead of handles or references.. ?
By ishakteyran in forum Advanced JavaReplies: 0Last Post: 12-28-2007, 12:25 PM -
Soft References
By rickcharles_b in forum Advanced JavaReplies: 0Last Post: 06-20-2007, 08:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks