Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.
Why I have an error
clone() has protected access in java.lang.Object
but not CloneNotSupportedException exception?
public class Test
{
public static void main(String[] args)
{
Test2 c1 = new Test2();
Test2 c2 = (Test2) c1.clone(); // error: clone() has protected access in java.lang.Object
}
}
class Test2
{
}
Because the error is at compile time.
Exception are at runtime. The program didn't even compile, so you didn't reach runtime.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.