CSC 216 F09/cloning1

From Expertiza_Wiki
Revision as of 14:26, 12 November 2009 by Twpensyl (talk | contribs)
Jump to navigation Jump to search

Shallow vs. Deep Cloning

Give the title of your exercise, which may include the name of the topic you are covering, or some other catchy title.

The problem

In Java, cloning an object creates a new object of the same class, copying each instance variable from the old object to the the new object. This sounds fairly straightforward, until you consider instance variables whose "values" are actually references to other, mutable objects. This creates the problem where modifying the clone can inadvertently modify the original object, or vice versa.

The solution to this problem is a recursive cloning function called a 'deep clone'. Instead of just copying values, this function calls itself on each value, excluding primitive data types.

Participants and props

The full exercise requires 4 students with cell phones, 4 sheets of paper or index cards, and a writing utensil.

The script

Describe how to do your exercise.