Results 1 to 6 of 6

Thread: Clone method in Java

  1. #1
    Join Date
    Jan 2009
    Posts
    85

    Clone method in Java

    Hi, I am beginner in java and wanted to know how to implement the clone method in java. Can anyone give me brief idea about it? I am waiting for your reply. Please, if you have any details about it, give it to me. So that I can increase my knowledge in java.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Clone method in Java

    Hi, I have a code which will help you to get some information about the clone method in java. I think you will able to get information about clone method in java from this.

    Code:
    import java.util.*;
    
    public class CloneJava
    {
      public static void main(String[] args)
    {
    
        Employee e = new Employee("Sanjay", 50000);
        e.setHireDay(2005,0,0);
        Employee e1 = (Employee)e.clone();
        e1.raiseSalary(20);
        e1.setHireDay(2008, 12, 31);
        System.out.println("Employee=" + e);
        System.out.println("copy=" + e1);
      }
    }
    class Employee implements Cloneable
    {
      public Employee(String str, double dou)
    {
        name = str;
        salary = dou;
      }
      public Object clone()
    {
        try
    {
          Employee eclone= (Employee)super.clone();
          eclone.hireDay = (Date)hireDay.clone();
          return eclone;
        }
        catch(CloneNotSupportedException e){
          System.out.println(e);
          return null;
        }
      }
      public void setHireDay(int year, int month, int day)
    {
        hireDay = new GregorianCalendar(year,month - 1, day).getTime();
      }
      public void raiseSalary(double byPercent)
    {
        double raise = salary * byPercent/100;
        salary += raise;
      }
      public String toString()
    {
        return "[name=" + name+ ",salary=" + salary+ ",hireDay=" + hireDay+ "]";
      }
      private String name;
      private double salary;
      private Date hireDay;
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Clone method in Java

    HI, I have got something on internet, regarding the clone method in java, I think this will help you in this case. I am not able to understand it, but if you able to understand it then it will help you.

    Code:
    import java.util.*;
    
    class Int 
    {
    private int j;  
    public Int(int jj)
    {
     j = jj; 
    }
    public void increment()
    {
     j++; 
    }
     
    public String toString() 
    {
        return Integer.toString(j);
    }
    }
    
    public class DemoCloning 
    {  
      public static void main(String[] args)
     {
    ArrayList al = new ArrayList();
    for(int j = 0; j < 10; j++ )
    al.add(new Int(j));
    System.out.println("al: " + al);
    ArrayList al1 = (ArrayList)al.clone();    
    for(Iterator e = al1.iterator(); e.hasNext(); 
    )  
    ((Int)e.next()).increment();
    System.out.println("al: " + al);
    }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Clone method in Java

    Hi, I have a code which is created by me, which will definitely provide you information about the clone method in simple way. Just make use of the following code to get the idea of clone method in java.

    Code:
    public class clone implements Cloneable 
    {
    private LinkedList names = new LinkedList();
    
    public clone() 
    {
            names.add("Alex");
            names.add("Melody");
            names.add("Jeff");
    }
    
    
    public String toString() 
    {
            StringBuffer sbuffer = new StringBuffer();
            Iterator i = names.iterator();
            while (i.hasNext()) 
    {
                sbuffer.append("\n\t" + i.next());
    }
            return sbuffer.toString();
    }
    
    
        public Object clone() 
    {
            try 
    {
                return super.clone();
    } 
    catch (CloneNotSupportedException e) 
    {
                throw new Error("This should not occur since we implement Cloneable");
     }
       }
    
    
        public Object deepClone() 
    {
            try 
    {
                clone cpy = (clone)super.clone();
                cpy.names = (LinkedList)names.clone();
                return cpy;
            }
     catch (CloneNotSupportedException e) 
    {
                throw new Error("This should not occur since we implement Cloneable");
            }
        }
    
        public boolean equals(Object object) 
    {
            if (object == this) 
    {
                return true;
            }
    
            if (object == null) 
    {
                return false;
            }
    
            if (!(this.getClass() == object.getClass())) 
    {
                return false;
            } 
    else 
    {
                clone tmp = (clone)object;
                if (this.names == tmp.names) 
    {
                    return true;
                } 
    else 
    {
                    return false;
                }
            }
            
        }
        
    
        public static void main(String[] args) 
    {
    
            clone clone1 = new clone();
            System.out.println("\nclone[1]\n" + "-----------------" + clone1);
    
            clone clone2 = (clone)clone1.clone();
            System.out.println("\nclone[2]\n" + "-----------------" + clone2);
    
            System.out.println("\nCompare Shallow Copy\n" + "--------------------\n" +"    clone1 == clone2      : " + (clone1 == clone2) + "\n" + "    clone1.equals(clone2) : " + clone1.equals(clone2));
    
            clone clone3 = (clone)clone1.deepClone();
            System.out.println("\nCompare Deep Copy\n" + "--------------------\n" + "    clone1 == clone3      : " + (clone1 == clone3) + "\n" + "    clone1.equals(clone3) : " + clone1.equals(clone3));
        }
    
    }

  5. #5
    Join Date
    Jan 2010
    Posts
    2

    Re: Clone method in Java

    What this does is clone an ArrayList object (making a shallow copy of its contents).

  6. #6
    Join Date
    Jan 2010
    Posts
    2

    Re: Clone method in Java

    Generally when overriding the equals() method of Object, it is good practice to override the hashCode() method as well. Such is the Java contract.

Similar Threads

  1. Problem in clone() method
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 02:17 AM
  2. What is SuppressWarnings method in java?
    By Juany in forum Software Development
    Replies: 4
    Last Post: 30-01-2010, 09:24 PM
  3. How to use valueOf method in java?
    By KAIRU26 in forum Software Development
    Replies: 4
    Last Post: 28-01-2010, 10:59 PM
  4. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM
  5. Java: How can I call one method in another method?
    By biohazard 76 in forum Software Development
    Replies: 3
    Last Post: 16-07-2009, 07:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,724,588.19183 seconds with 17 queries