<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FClass_Definition%2FClone</id>
		<title>Java Tutorial/Class Definition/Clone - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FClass_Definition%2FClone"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Class_Definition/Clone&amp;action=history"/>
		<updated>2026-04-22T00:15:35Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Class_Definition/Clone&amp;diff=4224&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Class_Definition/Clone&amp;diff=4224&amp;oldid=prev"/>
				<updated>2010-06-01T05:00:54Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:00, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Class_Definition/Clone&amp;diff=4223&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Class_Definition/Clone&amp;diff=4223&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  A collection of utilities to workaround limitations of Java clone framework ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * $HeadURL$&lt;br /&gt;
 * $Revision$&lt;br /&gt;
 * $Date$&lt;br /&gt;
 *&lt;br /&gt;
 *&lt;br /&gt;
 *  Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 *  contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 *  this work for additional information regarding copyright ownership.&lt;br /&gt;
 *  The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 *  (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 *  the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 *  Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 *  distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 *  See the License for the specific language governing permissions and&lt;br /&gt;
 *  limitations under the License.&lt;br /&gt;
 *&lt;br /&gt;
 * This software consists of voluntary contributions made by many&lt;br /&gt;
 * individuals on behalf of the Apache Software Foundation.  For more&lt;br /&gt;
 * information on the Apache Software Foundation, please see&lt;br /&gt;
 * &amp;lt;http://www.apache.org/&amp;gt;.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
import java.lang.reflect.InvocationTargetException;&lt;br /&gt;
import java.lang.reflect.Method;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * A collection of utilities to workaround limitations of Java clone framework.&lt;br /&gt;
 */&lt;br /&gt;
public class CloneUtils {&lt;br /&gt;
    public static Object clone(final Object obj) throws CloneNotSupportedException {&lt;br /&gt;
        if (obj == null) {&lt;br /&gt;
            return null;&lt;br /&gt;
        }&lt;br /&gt;
        if (obj instanceof Cloneable) {&lt;br /&gt;
            Class&amp;lt;?&amp;gt; clazz = obj.getClass ();&lt;br /&gt;
            Method m;&lt;br /&gt;
            try {&lt;br /&gt;
                m = clazz.getMethod(&amp;quot;clone&amp;quot;, (Class[]) null);&lt;br /&gt;
            } catch (NoSuchMethodException ex) {&lt;br /&gt;
                throw new NoSuchMethodError(ex.getMessage());&lt;br /&gt;
            }&lt;br /&gt;
            try {&lt;br /&gt;
                return m.invoke(obj, (Object []) null);&lt;br /&gt;
            } catch (InvocationTargetException ex) {&lt;br /&gt;
                Throwable cause = ex.getCause();&lt;br /&gt;
                if (cause instanceof CloneNotSupportedException) {&lt;br /&gt;
                    throw ((CloneNotSupportedException) cause); &lt;br /&gt;
                } else {&lt;br /&gt;
                    throw new Error(&amp;quot;Unexpected exception&amp;quot;, cause);&lt;br /&gt;
                }&lt;br /&gt;
            } catch (IllegalAccessException ex) {&lt;br /&gt;
                throw new IllegalAccessError(ex.getMessage());&lt;br /&gt;
            }&lt;br /&gt;
        } else {&lt;br /&gt;
            throw new CloneNotSupportedException();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * This class should not be instantiated.&lt;br /&gt;
     */&lt;br /&gt;
    private CloneUtils() {&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Arrays are automatically cloneable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    int[] ints = new int[] { 123, 234 };&lt;br /&gt;
    int[] intsClone = (int[]) ints.clone();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Cast after cloning ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class A {&lt;br /&gt;
  int l = 1;&lt;br /&gt;
}&lt;br /&gt;
class B extends A implements Cloneable {&lt;br /&gt;
  int m = 2;&lt;br /&gt;
}&lt;br /&gt;
class CloneDemo3 extends B {&lt;br /&gt;
  int n = 3;&lt;br /&gt;
  A a = new A();&lt;br /&gt;
  public static void main(String[] args) throws CloneNotSupportedException {&lt;br /&gt;
    CloneDemo3 c = new CloneDemo3();&lt;br /&gt;
    CloneDemo3 c2 = (CloneDemo3) c.clone();&lt;br /&gt;
    System.out.println(c.l);&lt;br /&gt;
    System.out.println(c2.l);&lt;br /&gt;
    System.out.println(c.m);&lt;br /&gt;
    System.out.println(c2.m);&lt;br /&gt;
    System.out.println(c.n);&lt;br /&gt;
    System.out.println(c2.n);&lt;br /&gt;
    System.out.println(c.a == c2.a);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Clone an array ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/* &lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 *  contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 *  this work for additional information regarding copyright ownership.&lt;br /&gt;
 *  The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 *  (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 *  the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 *  Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 *  distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 *  See the License for the specific language governing permissions and&lt;br /&gt;
 *  limitations under the License.&lt;br /&gt;
 *&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * @author Stephen Colebourne&lt;br /&gt;
 * @author Moritz Petersen&lt;br /&gt;
 * @author &lt;br /&gt;
 * @author Maarten Coene&lt;br /&gt;
 * @since 2.0&lt;br /&gt;
 * @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  // Clone&lt;br /&gt;
  //-----------------------------------------------------------------------&lt;br /&gt;
  /**&lt;br /&gt;
   * Shallow clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * The objects in the array are not cloned, thus there is no special&lt;br /&gt;
   * handling for multi-dimensional arrays.&lt;br /&gt;
   * &lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to shallow clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static Object[] clone(Object[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (Object[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static long[] clone(long[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (long[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static int[] clone(int[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (int[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static short[] clone(short[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (short[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static char[] clone(char[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (char[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static byte[] clone(byte[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (byte[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static double[] clone(double[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (double[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static float[] clone(float[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (float[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clones an array returning a typecast result and handling&lt;br /&gt;
   * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * This method returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input array.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to clone, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @return the cloned array, &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean[] clone(boolean[] array) {&lt;br /&gt;
      if (array == null) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
      return (boolean[]) array.clone();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Clone an object with clone method from parent ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    try {&lt;br /&gt;
      Employee e = new Employee(&amp;quot;Dolly&amp;quot;, 1000);&lt;br /&gt;
      System.out.println(e);&lt;br /&gt;
      System.out.println(&amp;quot;The employee&amp;quot;s name is &amp;quot; + e.getName());&lt;br /&gt;
      System.out.println(&amp;quot;The employees&amp;quot;s pay is &amp;quot; + e.getSalary());&lt;br /&gt;
      Employee eClone = (Employee) e.clone();&lt;br /&gt;
      System.out.println(eClone);&lt;br /&gt;
      System.out.println(&amp;quot;The clone&amp;quot;s name is &amp;quot; + eClone.getName());&lt;br /&gt;
      System.out.println(&amp;quot;The clones&amp;quot;s pay is &amp;quot; + eClone.getSalary());&lt;br /&gt;
      eClone.setName(&amp;quot;Polly&amp;quot;);&lt;br /&gt;
      eClone.setSalary(2000);&lt;br /&gt;
      System.out.println(&amp;quot;The employee&amp;quot;s name is &amp;quot; + e.getName());&lt;br /&gt;
      System.out.println(&amp;quot;The employees&amp;quot;s pay is &amp;quot; + e.getSalary());&lt;br /&gt;
      System.out.println(&amp;quot;The clone&amp;quot;s name is &amp;quot; + eClone.getName());&lt;br /&gt;
      System.out.println(&amp;quot;The clones&amp;quot;s pay is &amp;quot; + eClone.getSalary());&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      System.out.println(&amp;quot;Exception &amp;quot; + e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class Employee implements Cloneable {&lt;br /&gt;
  private StringBuffer name;&lt;br /&gt;
  private int salary;&lt;br /&gt;
  public Employee(String name, int salary) {&lt;br /&gt;
    this.name = new StringBuffer(name);&lt;br /&gt;
    this.salary = salary;&lt;br /&gt;
  }&lt;br /&gt;
  public Employee() {&lt;br /&gt;
  }&lt;br /&gt;
  public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
    try {&lt;br /&gt;
      return super.clone();&lt;br /&gt;
    } catch (CloneNotSupportedException cnse) {&lt;br /&gt;
      System.out.println(&amp;quot;CloneNotSupportedException thrown &amp;quot; + cnse);&lt;br /&gt;
      throw new CloneNotSupportedException();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public String getName() {&lt;br /&gt;
    return name.toString();&lt;br /&gt;
  }&lt;br /&gt;
  public void setName(String name) {&lt;br /&gt;
    this.name.delete(0, this.name.length());&lt;br /&gt;
    this.name.append(name);&lt;br /&gt;
  }&lt;br /&gt;
  public void setSalary(int salary) {&lt;br /&gt;
    this.salary = salary;&lt;br /&gt;
  }&lt;br /&gt;
  public int getSalary() {&lt;br /&gt;
    return this.salary;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Construct clone of an object ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class Box {&lt;br /&gt;
  double width;&lt;br /&gt;
  double height;&lt;br /&gt;
  double depth;&lt;br /&gt;
  Box(Box ob) { &lt;br /&gt;
    width = ob.width;&lt;br /&gt;
    height = ob.height;&lt;br /&gt;
    depth = ob.depth;&lt;br /&gt;
  }&lt;br /&gt;
  Box(double w, double h, double d) {&lt;br /&gt;
    width = w;&lt;br /&gt;
    height = h;&lt;br /&gt;
    depth = d;&lt;br /&gt;
  }&lt;br /&gt;
  Box() {&lt;br /&gt;
    width = -1; &lt;br /&gt;
    height = -1;&lt;br /&gt;
    depth = -1; &lt;br /&gt;
  }&lt;br /&gt;
  Box(double len) {&lt;br /&gt;
    width = height = depth = len;&lt;br /&gt;
  }&lt;br /&gt;
  double volume() {&lt;br /&gt;
    return width * height * depth;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class BoxWeight extends Box {&lt;br /&gt;
  double weight; &lt;br /&gt;
  BoxWeight(double w, double h, double d, double m) {&lt;br /&gt;
    width = w;&lt;br /&gt;
    height = h;&lt;br /&gt;
    depth = d;&lt;br /&gt;
    weight = m;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class DemoBoxWeight {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);&lt;br /&gt;
    BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);&lt;br /&gt;
    double vol;&lt;br /&gt;
    vol = mybox1.volume();&lt;br /&gt;
    System.out.println(vol);&lt;br /&gt;
    System.out.println(mybox1.weight);&lt;br /&gt;
    System.out.println();&lt;br /&gt;
    vol = mybox2.volume();&lt;br /&gt;
    System.out.println(vol);&lt;br /&gt;
    System.out.println(mybox2.weight);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  construct clone of an object and call constructor in the super class ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class Box {&lt;br /&gt;
  private double width;&lt;br /&gt;
  private double height;&lt;br /&gt;
  private double depth;&lt;br /&gt;
  Box(Box ob) { &lt;br /&gt;
    width = ob.width;&lt;br /&gt;
    height = ob.height;&lt;br /&gt;
    depth = ob.depth;&lt;br /&gt;
  }&lt;br /&gt;
  Box(double w, double h, double d) {&lt;br /&gt;
    width = w;&lt;br /&gt;
    height = h;&lt;br /&gt;
    depth = d;&lt;br /&gt;
  }&lt;br /&gt;
  Box() {&lt;br /&gt;
    width = -1; &lt;br /&gt;
    height = -1;&lt;br /&gt;
    depth = -1; &lt;br /&gt;
  }&lt;br /&gt;
  Box(double len) {&lt;br /&gt;
    width = height = depth = len;&lt;br /&gt;
  }&lt;br /&gt;
  double volume() {&lt;br /&gt;
    return width * height * depth;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class BoxWeight extends Box {&lt;br /&gt;
  double weight;&lt;br /&gt;
  BoxWeight(BoxWeight ob) { &lt;br /&gt;
    super(ob);&lt;br /&gt;
    weight = ob.weight;&lt;br /&gt;
  }&lt;br /&gt;
  BoxWeight(double w, double h, double d, double m) {&lt;br /&gt;
    super(w, h, d); &lt;br /&gt;
    weight = m;&lt;br /&gt;
  }&lt;br /&gt;
  BoxWeight() {&lt;br /&gt;
    super();&lt;br /&gt;
    weight = -1;&lt;br /&gt;
  }&lt;br /&gt;
  BoxWeight(double len, double m) {&lt;br /&gt;
    super(len);&lt;br /&gt;
    weight = m;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class DemoSuper {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);&lt;br /&gt;
    BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);&lt;br /&gt;
    BoxWeight mybox3 = new BoxWeight();&lt;br /&gt;
    BoxWeight mycube = new BoxWeight(3, 2);&lt;br /&gt;
    BoxWeight myclone = new BoxWeight(mybox1);&lt;br /&gt;
    double vol;&lt;br /&gt;
    vol = mybox1.volume();&lt;br /&gt;
    System.out.println(&amp;quot;Volume of mybox1 is &amp;quot; + vol);&lt;br /&gt;
    System.out.println(&amp;quot;Weight of mybox1 is &amp;quot; + mybox1.weight);&lt;br /&gt;
    System.out.println();&lt;br /&gt;
    vol = mybox2.volume();&lt;br /&gt;
    System.out.println(&amp;quot;Volume of mybox2 is &amp;quot; + vol);&lt;br /&gt;
    System.out.println(&amp;quot;Weight of mybox2 is &amp;quot; + mybox2.weight);&lt;br /&gt;
    System.out.println();&lt;br /&gt;
    vol = mybox3.volume();&lt;br /&gt;
    System.out.println(&amp;quot;Volume of mybox3 is &amp;quot; + vol);&lt;br /&gt;
    System.out.println(&amp;quot;Weight of mybox3 is &amp;quot; + mybox3.weight);&lt;br /&gt;
    System.out.println();&lt;br /&gt;
    vol = myclone.volume();&lt;br /&gt;
    System.out.println(&amp;quot;Volume of myclone is &amp;quot; + vol);&lt;br /&gt;
    System.out.println(&amp;quot;Weight of myclone is &amp;quot; + myclone.weight);&lt;br /&gt;
    System.out.println();&lt;br /&gt;
    vol = mycube.volume();&lt;br /&gt;
    System.out.println(&amp;quot;Volume of mycube is &amp;quot; + vol);&lt;br /&gt;
    System.out.println(&amp;quot;Weight of mycube is &amp;quot; + mycube.weight);&lt;br /&gt;
    System.out.println();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Copy an serializable object deeply ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.ByteArrayInputStream;&lt;br /&gt;
import java.io.ByteArrayOutputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.ObjectInputStream;&lt;br /&gt;
import java.io.ObjectOutputStream;&lt;br /&gt;
import java.io.Serializable;&lt;br /&gt;
/*&lt;br /&gt;
 * JBoss, Home of Professional Open Source&lt;br /&gt;
 * Copyright 2005, JBoss Inc., and individual contributors as indicated&lt;br /&gt;
 * by the @authors tag. See the copyright.txt in the distribution for a&lt;br /&gt;
 * full listing of individual contributors.&lt;br /&gt;
 *&lt;br /&gt;
 * This is free software; you can redistribute it and/or modify it&lt;br /&gt;
 * under the terms of the GNU Lesser General Public License as&lt;br /&gt;
 * published by the Free Software Foundation; either version 2.1 of&lt;br /&gt;
 * the License, or (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 * This software is distributed in the hope that it will be useful,&lt;br /&gt;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU&lt;br /&gt;
 * Lesser General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public&lt;br /&gt;
 * License along with this software; if not, write to the Free&lt;br /&gt;
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA&lt;br /&gt;
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  // ///////////////////////////////////////////////////////////////////////&lt;br /&gt;
  // Cloning Methods //&lt;br /&gt;
  // ///////////////////////////////////////////////////////////////////////&lt;br /&gt;
  /**&lt;br /&gt;
   * Copy an serializable object deeply.&lt;br /&gt;
   * &lt;br /&gt;
   * @param obj&lt;br /&gt;
   *          Object to copy.&lt;br /&gt;
   * @return Copied object.&lt;br /&gt;
   * &lt;br /&gt;
   * @throws IOException&lt;br /&gt;
   * @throws ClassNotFoundException&lt;br /&gt;
   */&lt;br /&gt;
  public static Object copy(final Serializable obj) throws IOException, ClassNotFoundException {&lt;br /&gt;
    ObjectOutputStream out = null;&lt;br /&gt;
    ObjectInputStream in = null;&lt;br /&gt;
    Object copy = null;&lt;br /&gt;
    try {&lt;br /&gt;
      // write the object&lt;br /&gt;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();&lt;br /&gt;
      out = new ObjectOutputStream(baos);&lt;br /&gt;
      out.writeObject(obj);&lt;br /&gt;
      out.flush();&lt;br /&gt;
      // read in the copy&lt;br /&gt;
      byte data[] = baos.toByteArray();&lt;br /&gt;
      ByteArrayInputStream bais = new ByteArrayInputStream(data);&lt;br /&gt;
      in = new ObjectInputStream(bais);&lt;br /&gt;
      copy = in.readObject();&lt;br /&gt;
    } finally {&lt;br /&gt;
      out.close();&lt;br /&gt;
      in.close();&lt;br /&gt;
    }&lt;br /&gt;
    return copy;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Copying Objects implements Cloneable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class Flea implements Cloneable {&lt;br /&gt;
  public Flea() {&lt;br /&gt;
  }&lt;br /&gt;
  public void setName(String aName) {&lt;br /&gt;
    name = aName;&lt;br /&gt;
  }&lt;br /&gt;
  public String getName() {&lt;br /&gt;
    return name;&lt;br /&gt;
  }&lt;br /&gt;
  public String getSpecies() {&lt;br /&gt;
    return species;&lt;br /&gt;
  }&lt;br /&gt;
  public void sound() {&lt;br /&gt;
    System.out.println(&amp;quot;Psst&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  public String toString() {&lt;br /&gt;
    return super.toString() + &amp;quot;\nIt&amp;quot;s &amp;quot; + name + &amp;quot; the &amp;quot; + species;&lt;br /&gt;
  }&lt;br /&gt;
  public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
    return super.clone();&lt;br /&gt;
  }&lt;br /&gt;
  private String name;&lt;br /&gt;
  private String species;&lt;br /&gt;
}&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    try {&lt;br /&gt;
      Flea myPet = new Flea();&lt;br /&gt;
      myPet.setName(&amp;quot;my name&amp;quot;);&lt;br /&gt;
      Flea yourPet = (Flea) myPet.clone();&lt;br /&gt;
      yourPet.setName(&amp;quot;Gnasher&amp;quot;);&lt;br /&gt;
      System.out.println(&amp;quot;\nYour pet details:\n&amp;quot; + yourPet);&lt;br /&gt;
      System.out.println(&amp;quot;\nMy pet details:\n&amp;quot; + myPet);&lt;br /&gt;
    } catch (CloneNotSupportedException e) {&lt;br /&gt;
      e.printStackTrace(System.err);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Your pet details:&lt;br /&gt;
Flea@360be0&lt;br /&gt;
It&amp;quot;s Gnasher the null&lt;br /&gt;
My pet details:&lt;br /&gt;
Flea@45a877&lt;br /&gt;
It&amp;quot;s my name the null&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Deep clone Object ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (C) 2001-2003 Colin Bell&lt;br /&gt;
 * colbell@users.sourceforge.net&lt;br /&gt;
 *&lt;br /&gt;
 * This library is free software; you can redistribute it and/or&lt;br /&gt;
 * modify it under the terms of the GNU Lesser General Public&lt;br /&gt;
 * License as published by the Free Software Foundation; either&lt;br /&gt;
 * version 2.1 of the License, or (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 * This library is distributed in the hope that it will be useful,&lt;br /&gt;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU&lt;br /&gt;
 * Lesser General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public&lt;br /&gt;
 * License along with this library; if not, write to the Free Software&lt;br /&gt;
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA&lt;br /&gt;
 */&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import java.text.NumberFormat;&lt;br /&gt;
import java.util.regex.Matcher;&lt;br /&gt;
import java.util.regex.Pattern;&lt;br /&gt;
/**&lt;br /&gt;
 * General purpose utilities functions.&lt;br /&gt;
 *&lt;br /&gt;
 * @author &lt;br /&gt;
 */&lt;br /&gt;
public class Utilities&lt;br /&gt;
{&lt;br /&gt;
  &lt;br /&gt;
  /**&lt;br /&gt;
   * Creates a clone of any serializable object. Collections and arrays&lt;br /&gt;
   * may be cloned if the entries are serializable.&lt;br /&gt;
   *&lt;br /&gt;
   * Caution super class members are not cloned if a super class is not serializable.&lt;br /&gt;
   */&lt;br /&gt;
  public static Object cloneObject(Object toClone, final ClassLoader classLoader)&lt;br /&gt;
  {&lt;br /&gt;
     if(null == toClone)&lt;br /&gt;
     {&lt;br /&gt;
        return null;&lt;br /&gt;
     }&lt;br /&gt;
     else&lt;br /&gt;
     {&lt;br /&gt;
        try&lt;br /&gt;
        {&lt;br /&gt;
           ByteArrayOutputStream bOut = new ByteArrayOutputStream();&lt;br /&gt;
           ObjectOutputStream oOut = new ObjectOutputStream(bOut);&lt;br /&gt;
           oOut.writeObject(toClone);&lt;br /&gt;
           oOut.close();&lt;br /&gt;
           ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());&lt;br /&gt;
           bOut.close();&lt;br /&gt;
           ObjectInputStream oIn = new ObjectInputStream(bIn)&lt;br /&gt;
           {&lt;br /&gt;
              protected Class&amp;lt;?&amp;gt; resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException&lt;br /&gt;
              {&lt;br /&gt;
                return Class.forName(desc.getName(), false, classLoader);&lt;br /&gt;
              }&lt;br /&gt;
           };&lt;br /&gt;
           bIn.close();&lt;br /&gt;
           Object copy = oIn.readObject();&lt;br /&gt;
           oIn.close();&lt;br /&gt;
           return copy;&lt;br /&gt;
        }&lt;br /&gt;
        catch (Exception e)&lt;br /&gt;
        {&lt;br /&gt;
           throw new RuntimeException(e);&lt;br /&gt;
        }&lt;br /&gt;
     }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Deep clone serializing/de-serializng Clone ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 * contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 * this work for additional information regarding copyright ownership.&lt;br /&gt;
 * The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 * (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 * the License.  You may obtain a copy of the License at&lt;br /&gt;
 * &lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 * &lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.ByteArrayInputStream;&lt;br /&gt;
import java.io.ByteArrayOutputStream;&lt;br /&gt;
import java.io.ObjectInputStream;&lt;br /&gt;
import java.io.ObjectOutputStream;&lt;br /&gt;
/**&lt;br /&gt;
 * Utility for object cloning&lt;br /&gt;
 * &lt;br /&gt;
 * @author &lt;br /&gt;
 */&lt;br /&gt;
public class CloneUtil&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Provides a deep clone serializing/de-serializng &amp;lt;code&amp;gt;objToClone&amp;lt;/code&amp;gt;     &lt;br /&gt;
     * &lt;br /&gt;
     * @param objToClone The object to be cloned&lt;br /&gt;
     * @return The cloned object&lt;br /&gt;
     */&lt;br /&gt;
    public static final Object deepClone(Object objToClone)&lt;br /&gt;
    {&lt;br /&gt;
        try&lt;br /&gt;
        {&lt;br /&gt;
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(100);&lt;br /&gt;
            ObjectOutputStream objectoutputstream = new ObjectOutputStream(bytearrayoutputstream);&lt;br /&gt;
            objectoutputstream.writeObject(objToClone);&lt;br /&gt;
            byte abyte0[] = bytearrayoutputstream.toByteArray();&lt;br /&gt;
            objectoutputstream.close();&lt;br /&gt;
            ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);&lt;br /&gt;
            ObjectInputStream objectinputstream = new ObjectInputStream(bytearrayinputstream);&lt;br /&gt;
            Object clone = objectinputstream.readObject();&lt;br /&gt;
            objectinputstream.close();&lt;br /&gt;
            return clone;&lt;br /&gt;
        }&lt;br /&gt;
        catch (Exception exception)&lt;br /&gt;
        {&lt;br /&gt;
            // nothing&lt;br /&gt;
        }&lt;br /&gt;
        return null;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Demonstrate the clone() method. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class TestClone implements Cloneable {&lt;br /&gt;
  int a;&lt;br /&gt;
  double b;&lt;br /&gt;
  TestClone cloneTest() {&lt;br /&gt;
    try {&lt;br /&gt;
      return (TestClone) super.clone();&lt;br /&gt;
    } catch (CloneNotSupportedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Cloning not allowed.&amp;quot;);&lt;br /&gt;
      return this;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class CloneDemo {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    TestClone x1 = new TestClone();&lt;br /&gt;
    TestClone x2;&lt;br /&gt;
    x1.a = 10;&lt;br /&gt;
    x1.b = 20.98;&lt;br /&gt;
    x2 = x1.cloneTest(); // clone x1&lt;br /&gt;
    System.out.println(&amp;quot;x1: &amp;quot; + x1.a + &amp;quot; &amp;quot; + x1.b);&lt;br /&gt;
    System.out.println(&amp;quot;x2: &amp;quot; + x2.a + &amp;quot; &amp;quot; + x2.b);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Manipulate properties after clone operation ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    try {&lt;br /&gt;
      Employee e = new Employee(&amp;quot;B&amp;quot;, 1000);&lt;br /&gt;
      System.out.println(e);&lt;br /&gt;
      System.out.println(&amp;quot;The employee&amp;quot;s name is &amp;quot; + e.getName());&lt;br /&gt;
      System.out.println(&amp;quot;The employees&amp;quot;s pay is &amp;quot; + e.getSalary());&lt;br /&gt;
      Employee eClone = (Employee) e.clone();&lt;br /&gt;
      System.out.println(eClone);&lt;br /&gt;
      System.out.println(&amp;quot;The clone&amp;quot;s name is &amp;quot; + eClone.getName());&lt;br /&gt;
      System.out.println(&amp;quot;The clones&amp;quot;s pay is &amp;quot; + eClone.getSalary());&lt;br /&gt;
      eClone.setName(&amp;quot;A&amp;quot;);&lt;br /&gt;
      eClone.setSalary(2000);&lt;br /&gt;
      System.out.println(&amp;quot;The employee&amp;quot;s name is &amp;quot; + e.getName());&lt;br /&gt;
      System.out.println(&amp;quot;The employees&amp;quot;s pay is &amp;quot; + e.getSalary());&lt;br /&gt;
      System.out.println(&amp;quot;The clone&amp;quot;s name is &amp;quot; + eClone.getName());&lt;br /&gt;
      System.out.println(&amp;quot;The clones&amp;quot;s pay is &amp;quot; + eClone.getSalary());&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      System.out.println(&amp;quot;Exception &amp;quot; + e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class Employee implements Cloneable {&lt;br /&gt;
  private StringBuffer name;&lt;br /&gt;
  private int salary;&lt;br /&gt;
  public Employee(String name, int salary) {&lt;br /&gt;
    this.name = new StringBuffer(name);&lt;br /&gt;
    this.salary = salary;&lt;br /&gt;
  }&lt;br /&gt;
  public Employee() {&lt;br /&gt;
  }&lt;br /&gt;
  public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
    try {&lt;br /&gt;
      Employee o = (Employee) super.clone();&lt;br /&gt;
      o.name = new StringBuffer(name.toString());&lt;br /&gt;
      return o;&lt;br /&gt;
    } catch (CloneNotSupportedException cnse) {&lt;br /&gt;
      System.out.println(&amp;quot;CloneNotSupportedException thrown &amp;quot; + cnse);&lt;br /&gt;
      throw new CloneNotSupportedException();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public String getName() {&lt;br /&gt;
    return name.toString();&lt;br /&gt;
  }&lt;br /&gt;
  public void setName(String name) {&lt;br /&gt;
    this.name.delete(0, this.name.length());&lt;br /&gt;
    this.name.append(name);&lt;br /&gt;
  }&lt;br /&gt;
  public void setSalary(int salary) {&lt;br /&gt;
    this.salary = salary;&lt;br /&gt;
  }&lt;br /&gt;
  public int getSalary() {&lt;br /&gt;
    return this.salary;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Override the clone() method. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class TestClone implements Cloneable {&lt;br /&gt;
  int a;&lt;br /&gt;
  double b;&lt;br /&gt;
  public Object clone() {&lt;br /&gt;
    try {&lt;br /&gt;
      return super.clone();&lt;br /&gt;
    } catch (CloneNotSupportedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Cloning not allowed.&amp;quot;);&lt;br /&gt;
      return this;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class CloneDemo2 {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    TestClone x1 = new TestClone();&lt;br /&gt;
    TestClone x2;&lt;br /&gt;
    x1.a = 10;&lt;br /&gt;
    x1.b = 20.98;&lt;br /&gt;
    x2 = (TestClone) x1.clone();&lt;br /&gt;
    System.out.println(&amp;quot;x1: &amp;quot; + x1.a + &amp;quot; &amp;quot; + x1.b);&lt;br /&gt;
    System.out.println(&amp;quot;x2: &amp;quot; + x2.a + &amp;quot; &amp;quot; + x2.b);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  public Object clone() throws CloneNotSupportedException ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws CloneNotSupportedException {&lt;br /&gt;
    CloneDemo2 cd2 = new CloneDemo2();&lt;br /&gt;
    System.out.println(cd2.salary);&lt;br /&gt;
    AnotherClass ac = new AnotherClass();&lt;br /&gt;
    System.out.println(ac.gradeLetter);&lt;br /&gt;
    AnotherClass y = (AnotherClass) ac.clone();&lt;br /&gt;
    System.out.println(y.gradeLetter);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class CloneDemo2 implements Cloneable {&lt;br /&gt;
  double salary = 50000.0;&lt;br /&gt;
}&lt;br /&gt;
class AnotherClass implements Cloneable {&lt;br /&gt;
  char gradeLetter = &amp;quot;C&amp;quot;;&lt;br /&gt;
  public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
    return super.clone();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Serializable Clone ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.ByteArrayInputStream;&lt;br /&gt;
import java.io.ByteArrayOutputStream;&lt;br /&gt;
import java.io.ObjectInputStream;&lt;br /&gt;
import java.io.ObjectOutputStream;&lt;br /&gt;
public abstract class SerializableClone {&lt;br /&gt;
  public static Object clone(final Object obj) throws Exception {&lt;br /&gt;
    ByteArrayOutputStream out = new ByteArrayOutputStream();&lt;br /&gt;
    ObjectOutputStream oout = new ObjectOutputStream(out);&lt;br /&gt;
    oout.writeObject(obj);&lt;br /&gt;
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));&lt;br /&gt;
    return in.readObject();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Shallow copy then deep copy ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class A {&lt;br /&gt;
  int l = 1;&lt;br /&gt;
}&lt;br /&gt;
class B extends A implements Cloneable {&lt;br /&gt;
  int m = 2;&lt;br /&gt;
}&lt;br /&gt;
class CloneDemo4 extends B {&lt;br /&gt;
  int n = 3;&lt;br /&gt;
  A a = new A();&lt;br /&gt;
  public static void main(String[] args) throws CloneNotSupportedException {&lt;br /&gt;
    CloneDemo4 c = new CloneDemo4();&lt;br /&gt;
    CloneDemo4 c2 = (CloneDemo4) c.clone();&lt;br /&gt;
    System.out.println(c.l);&lt;br /&gt;
    System.out.println(c2.l);&lt;br /&gt;
    System.out.println(c.m);&lt;br /&gt;
    System.out.println(c2.m);&lt;br /&gt;
    System.out.println(c.n);&lt;br /&gt;
    System.out.println(c2.n);&lt;br /&gt;
    System.out.println(c.a == c2.a);&lt;br /&gt;
  }&lt;br /&gt;
  protected Object clone() throws CloneNotSupportedException {&lt;br /&gt;
    // First, perform a shallow copy.&lt;br /&gt;
    CloneDemo4 temp = (CloneDemo4) super.clone();&lt;br /&gt;
    if (a != null)&lt;br /&gt;
      temp.a = new A();&lt;br /&gt;
    return temp;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Uses serialization to perform deep copy cloning. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.ByteArrayInputStream;&lt;br /&gt;
import java.io.ByteArrayOutputStream;&lt;br /&gt;
import java.io.ObjectInputStream;&lt;br /&gt;
import java.io.ObjectOutputStream;&lt;br /&gt;
import java.io.Serializable;&lt;br /&gt;
public class Main implements Cloneable, Serializable {&lt;br /&gt;
  public Object clone() {&lt;br /&gt;
    Object clonedObj = null;&lt;br /&gt;
    try {&lt;br /&gt;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();&lt;br /&gt;
      ObjectOutputStream oos = new ObjectOutputStream(baos);&lt;br /&gt;
      oos.writeObject(this);&lt;br /&gt;
      oos.close();&lt;br /&gt;
      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());&lt;br /&gt;
      ObjectInputStream ois = new ObjectInputStream(bais);&lt;br /&gt;
      clonedObj = ois.readObject();&lt;br /&gt;
      ois.close();&lt;br /&gt;
    } catch (Exception cnfe) {&lt;br /&gt;
      System.out.println(&amp;quot;Class not found &amp;quot; + cnfe);&lt;br /&gt;
    }&lt;br /&gt;
    return clonedObj;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>