Inspectopedia 2025.2 Help

'clone()' should have return type equal to the class it contains

Reports clone() methods with return types different from the class they're located in.

Often a clone() method will have a return type of java.lang.Object, which makes it harder to use by its clients. Effective Java (the second and third editions) recommends making the return type of the clone() method the same as the class type of the object it returns.

Example:

class Foo implements Cloneable { public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { throw new AssertionError(); } } }

After the quick-fix is applied:

class Foo implements Cloneable { public Foo clone() { try { return (Foo)super.clone(); } catch (CloneNotSupportedException e) { throw new AssertionError(); } } }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

CloneReturnsClassType
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Cloning issues

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection CloneReturnsClassType

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

IntelliJ IDEA 2025.2, Qodana for JVM 2025.2,

Last modified: 18 September 2025