• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clone problems

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
i'm using the clone method, it's giving me an error while i copile
it's saying that clone have a private access!
how can i fix that is there another way to clone an abject, (it's like a hashMap)
thanks
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


it's saying that clone have a private access!


Well, just don't implement your clone method with the private access modifier.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make a class publically cloneable, you have to do two things:

1) Declare that the class implements java.lang.Cloneable

2) Override clone() something like this:



That's what you need to do, and that's how it's supposed to work. Is this a good design? No. But that's how it works.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The clone method cames from the java.lang.Object class (where all objects extends). Here is the signature of the method:

protected Object clone() throws CloneNotSupportedException

When you override this method you are not allowed to restrict its visibility (e.g. "private"), but the contrary is permitted: you can make it "public".

Wagner Danda
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, got it thanks,
but the thing is that my main class does not implement the clone method, so i cant use the super.clone();

2) Override clone() something like this:
code:
--------------------------------------------------------------------------------
public Object clone() throws CloneNotSupportedException { return super.clone();}
--------------------------------------------------------------------------------



maybe ill have to create my own clone method from nothing, and copy all the variables
thanks for the help
Omar
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by omar bili:
ok, got it thanks,
but the thing is that my main class does not implement the clone method, so i cant use the super.clone();



maybe ill have to create my own clone method from nothing, and copy all the variables
thanks for the help
Omar



In order to support clone(), you ALWAYS have to create your own clone method. However, you only need to copy the variables manually if you need to perform a "deep clone". If you just need a shallow clone, then you should call super.clone() as shown. Note that this typically calls the default clone() method from Object.

Layne
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic