7
import java.util.*; import java.lang.*; public class Test{ public static void main(String[] argv){ String s1="abc"; String s2=(String) s1.clone(); } } 

Why this simple test program doesn't work?

2
  • 6
    In what way doesn't it work? Commented Feb 6, 2012 at 17:10
  • Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.CloneNotSupportedException; must be caught or declared to be thrown at test.Test.main(Test.java:9) Java Result: 1 Commented Feb 6, 2012 at 17:12

5 Answers 5

20

clone is a method of the Object class. For a class to be "cloneable" it should implement the marker Cloneable interface. String class doesn't implement this interface and doesn't override the clone method hence the error.

I hope the above snippet is for educational purposes because you should never feel a need to call clone on strings in Java given that:

  1. Strings in Java are immutable. Feel free to share them across methods/classes
  2. There already exists a constructor new String(String) which acts like a copy constructor and is pretty much equivalent to your clone() call.
Sign up to request clarification or add additional context in comments.

2 Comments

Effective Java item 11: Override clone judiciously. (Or even better, just use copy constructors and the like other than clone.)
It doesn't compile. clone isn't a public method in Cloneable, so implementing that wouldn't matter.
6

Object.clone() is protected. It is a tricky API to use.

Usually one exposes clone() when one extends Object by broadening the method's visibility.

Clone on any string has little meaning, since it is both final and immutable.

There is a reason to copy a string; that can be done with:

String s1 = ...; String s2 = new String(s1) 

1 Comment

It's rare to need to copy a string, and you haven't explained why one would (this question does).
1

clone() is a protected method on the Object class. If you want a class to be cloneable the general pattern is to implement Cloneable and make that method public.

Comments

1

It obviously couldn't be compiled. Object.clone has protected access.

Beyond being accessible within the class itself and to code within the same package..., a protected member can also be accessed from a class through object references that are of at least the same type as the class

Comments

0

For a class to be "cloneable" it should implement the marker Cloneable interface. String class doesn't implement this interface and doesn't override the clone method hence the error.

protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object.

Strings in Java are immutable. Feel free to share them across methods/classes There already exists a constructor new String(String) which acts like a copy constructor and is pretty much equivalent to your clone() call.

Usually one exposes clone() when one extends Object by broadening the method's visibility.

Clone on any string has little meaning, since it is both final and immutable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.