|
|
|
| Since you can't call the Object class's clone() method directly, you might think that you could implement your own clone method in your class, and then have it call the Object class's clone() method. So let's try that:
However, when we try to compile this code, the Java compiler produces the following compile-time error: "CloneMe.java": unreported exception java.lang.CloneNotSupportedException;
must be caught or declared to be thrown at line 18, column 23
It turns out that the clone method is declared as follows: protected Object clone() throws CloneNotSupportedException So we have to modify our clone() method to conform to the Object class's signature:
Now it compiles! So let's test it:
Note that I had to add the throws CloneNotSupportedException to get the above to compile. (This relates to the rules governing how exceptions are handled, which we haven't talked about yet. Just take this change on faith, at least for the moment.) OK, the above compiles. What happens when we run it? We get the following: Exception in thread "main" java.lang.CloneNotSupportedException: CloneMe at java.lang.Object.clone(Native Method) at CloneMe.clone(CloneMe.java:18) at CloneMeTest.main(CloneMeTest.java:25) What's wrong, now?
|
|
This page was last modified on 02 October, 2007 |