CKTIS / Java
lang.Cloneable: practical reference
lang.Cloneable: practical reference: Cloneable is a marker checked by Object.clone; it provides no public clone method and default cloning is shallow.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
java.lang.CloneableCloneable is a marker checked by Object.clone; it provides no public clone method and default cloning is shallow.
Use Cloneable only with an explicitly designed copying contract; arrays support it and clone to the same array type.
It is a marker with no clone method. Object.clone is shallow, so references to mutable fields remain shared.
Object.clone on a non-Cloneable object throws CloneNotSupportedException, and cloning does not execute constructors.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
int[] original = {1, 2};
int[] copy = original.clone();
System.out.println(copy instanceof Cloneable);
}
}
Result
true
The example exercises the documented element in a small deterministic program and compares exact standard output.