Fedora Linux Support Community & Resources Center
  #1  
Old 16th July 2012, 06:40 PM
pushparaj_bits Offline
Registered User
 
Join Date: Oct 2009
Posts: 38
windows_7firefox
issue with Java.lang.Cloneable interface guidelines

The documentation for the Cloneable says -

Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

But the following code is working correctly. It is not giving any error when I call ex1.clone.

package com.sriPushpa.thread;

public class exceptionHandling implements Cloneable {
int a = 10;

public static void main(String args[]) {
exceptionHandling ex1 = new exceptionHandling();
exceptionHandling ex2 = null;
try {
ex2 = (exceptionHandling) ex1.clone();

} catch (CloneNotSupportedException e) {

e.printStackTrace();
}
System.out.println("SUCCESS");
}

}
Reply With Quote
  #2  
Old 27th July 2012, 07:52 AM
pushparaj_bits Offline
Registered User
 
Join Date: Oct 2009
Posts: 38
windows_xp_2003firefox
Re: issue with Java.lang.Cloneable interface guidelines

Hi,

I have understood the reason.... I got answers from stackOverflow, which I am pasting here , which may be helpful to others..

The clone() method is implemented by java.lang.Object, as a protected method. Your code works because you're calling the clone() method from the same class as the one you clone.

If you want your object to be cloneable, you should override the clone() method and make it public:

public Object clone() throws CloneNotSupportedException {
return super.clone();
}

Note that the doc doesn't say that implementing the Cloneable interface will always make clone() fail. It says that implementing it is not a guarantee that clone() will work. This is very different.


and


The root class java.lang.Object contains the method clone(). By default, if the concrete class implements Cloneable, the default class implementation makes a shallow copy.

From the method documentation:

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. ... Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.


and
Reply With Quote
Reply

Tags
guidelines, interface, javalangcloneable

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
java/lang/NoClassDefFoundError: java/lang/Object kc0olm Using Fedora 2 19th December 2011 11:26 AM
java.lang.NoSuchMethodError googlehelper Programming & Packaging 1 10th January 2009 07:49 PM
java.lang.UnsupportedClassVersionError JurB Programming & Packaging 1 23rd January 2008 11:00 AM
java.lang.NoSuchMethodError tech291083 Programming & Packaging 6 30th August 2007 02:23 PM
java.lang.NoSuchMethodError: org.w3c.dom.Node.getTextContent()Ljava/lang/String; Itsman_de Programming & Packaging 2 17th January 2006 10:31 PM


Current GMT-time: 15:55 (Sunday, 19-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat