File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clone ( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clone ( )" Watch "clone ( )" New topic
Author

clone ( )

catchbollu
Greenhorn

Joined: Jul 27, 2000
Posts: 5
cant i clone a string object like this below:
the compiler is giving an error like this:
stringfunctions.java:9: Can't access protected method clone in class java.lang.O
bject. java.lang.String is not a subclass of the current class.
String s4=(String)s2.clone();
^
why???
public class stringfunctions
{
public static void main(String args[])
{
String s2="bhushan";
String s4=(String)s2.clone();
System.out.println("strings are......." +s2.equals(s4));
}
}


catchbollu entered in
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
You cannot access protected members outside the class. The Object class is defined in the java.lang package which is different than the package that defines your class.
BTW, your name "catchbollu" does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Take a look at one of the old discussions on a similar topic.
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java2 Platform.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
String does not implement the interface Cloneable.
String only inherits from Object. Object does not implement the interface Cloneable. Only Classes which implement the Cloneable interface can clone() stuff.
From the API:

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. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. The clone method is implemented by the class Object as a convenient, general utility for subclasses that implement the interface Cloneable, possibly also overriding the clone method, in which case the overriding definition can refer to this utility definition by the call:
super.clone()


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clone ( )