Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Advice on Cloneable / clone()
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 2nd November 21:42
scherzin
External User
 
Posts: 1
Default Advice on Cloneable / clone()



Hi,

I'm trying to pop an object from a stack and clone it without knowing
anything more about its type than the object implementing interface
Cloneable:

// S is a stack

Object o1 = S.peek();
Object o2 = ((Clonable) o1).clone();

Yet somehow, this does not work. Does anybody know why?

Thanks so much,
Stef
  Reply With Quote


 


2 2nd November 21:43
kristian bisgaard lassen
External User
 
Posts: 1
Default Advice on Cloneable / clone()



Hi Stef,

Clone does not work on an arbitrary Java object which inherits from
Object. The object has to implement the Cloneable interface in the Java
APIT to make it work. I guess this is because everything in Java is
references so you can not tell whether or not an object is part-of or
reference-of object in a class. Hence the javac does not try to be smart
(which is good) and leave it up to the programmer to make it explicit what
he thinks is the semantic meaning of cloning his/her class.

Best Regards,
Kristian
  Reply With Quote


 


3 11th November 21:26
daniel_sjöblom
External User
 
Posts: 1
Default Advice on Cloneable / clone()


Because the clone method is not visible. The clone() method is not a
member of the Cloneable interface.

--
Daniel Sjöblom
  Reply With Quote
Reply


Thread Tools
Display Modes




666