Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Object copying
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== In Java === The following presents examples for one of the most widely used object-oriented languages, [[Java (programming language)|Java]], which should cover nearly every way that an object-oriented language can treat this problem. Unlike in C++, objects in Java are always accessed indirectly through [[reference (computer science)|references]]. Objects are never created implicitly but instead are always passed or assigned by a reference variable. (Methods in Java are always ''pass by value'', however, it is the value of the reference variable that is being passed.)<ref name="Passing Information to a Method or a Constructor">{{cite web|title=Passing Information to a Method or a Constructor|url=http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html|access-date=8 October 2013}}</ref> The [[Java virtual machine|Java Virtual Machine]] manages [[garbage collection (computer science)|garbage collection]] so that objects are cleaned up after they are no longer reachable. There is no automatic way to copy any given object in Java. Copying is usually performed by a [[clone (Java method)|clone() method]] of a class. This method usually, in turn, calls the clone() method of its parent class to obtain a copy, and then does any custom copying procedures. Eventually this gets to the clone() method of <code>Object</code> (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy"). If this method is used, the class must implement the {{Javadoc:SE|java/lang|Cloneable}} marker interface, or else it will [[exception handling|throw]] a "Clone Not Supported Exception". After obtaining a copy from the parent class, a class' own clone() method may then provide custom cloning capability, like deep copying (i.e. duplicate some of the structures referred to by the object) or giving the new instance a new unique ID. The return type of clone() is <code>Object</code>, but implementers of a clone method could write the type of the object being cloned instead due to Java's support for [[covariant return types]]. One advantage of using clone() is that since it is an [[method overriding (programming)|overridable method]], we can call clone() on any object, and it will use the clone() method of its class, without the calling code needing to know what that class is (which would be needed with a copy constructor). A disadvantage is that one often cannot access the clone() method on an abstract type. Most [[interface (object-oriented programming)|interfaces]] and [[abstract class]]es in Java do not specify a public clone() method. Thus, often the only way to use the clone() method is if the class of an object is known, which is contrary to the abstraction principle of using the most generic type possible. For example, if one has a List reference in Java, one cannot invoke clone() on that reference because List specifies no public clone() method. Implementations of List like Array List and Linked List all generally have clone() methods, but it is inconvenient and bad abstraction to carry around the class type of an object. Another way to copy objects in Java is to [[serialization|serialize]] them through the {{Javadoc:SE|java/io|Serializable}} interface. This is typically used for [[persistence (computer science)|persistence]] and [[wire protocol]] purposes, but it does create copies of objects and, unlike clone, a deep copy that gracefully handles cycled graphs of objects is readily available with minimal effort from a programmer. Both of these methods suffer from a notable problem: the [[constructor (computer science)|constructor]] is not used for objects copied with clone or serialization. This can lead to bugs with improperly initialized data, prevents the use of [[final (Java)|<code>final</code>]] member fields, and makes maintenance challenging. Some utilities attempt to overcome these issues by using reflection to deep copy objects, such as the deep-cloning library.<ref>[https://code.google.com/p/cloning/ Java deep-cloning library]</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)