Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I can't clone an object

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I can't clone an object

    Sorry for a possibly stupid question in advance: I am just a beginner and just study Java. The compiler gives clonenotsupportedexception for the House house2 = (House)house1.clone() part of the code. I do not understand why as the House class implements the Cloneable interface. Thank you in advance for your hints and advice.

    public class House implements Cloneable, Comparable {
        private int id;
        private double area;
        private java.util.Date whenBuilt;
     
        public House(int id, double area) {
            this.id = id;
            this.area = area;
            whenBuilt = new java.util.Date();
        }
     
        public double getId() {
            return id;
        }
     
        public double getArea() {
            return area;
        }
     
        public java.util.Date getWhenBuilt() {
            return whenBuilt;
        }
     
        /** Override the protected clone method defined in the Object
         class, and strengthen its accessibility */
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
     
        /** Implement the compareTo method defined in Comparable */
        public int compareTo(Object o) {
            if (area > ((House)o).area)
                return 1;
            else if (area < ((House)o).area)
                return -1;
            else
                return 0;
        }
    }
    class Implement
    {
     
     
        public static void main (String [] args)
     
        {
            House house1 = new House(1, 1750.50);
            House house2 = (House)house1.clone();
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I can't clone an object

    Please post the full text of the compiler's error message.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: I can't clone an object

    use try and catch Exception instead of the throwing one. I tried your code with try and catch and it worked. So use
    try
    {
    }catch(Exception e)
    {
    return this;
    }

    this should work

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't clone an object

    Norm and myjava! Thank you very much for your help! I was just inattentive and didn't read carefully enough what the compiler really told me. May be it was my greatest mistake. And now the answer seems so evident to me!

Similar Threads

  1. How to clone a singleton class?
    By Pratyush in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 4th, 2013, 02:27 AM
  2. Making an Alchemy/Doodle God clone in Java
    By sapientij in forum Java Theory & Questions
    Replies: 1
    Last Post: October 29th, 2012, 07:42 AM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. Object.clone()
    By bbr201 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 14th, 2010, 10:55 AM
  5. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM