Jtest logo




Contents  Previous  Next  Index

MISC.CLNC


Avoid using a constructor for implementing 'clone()'

Description

This rule flags code that uses a constructor for implementing `clone()'.

Using a constructor when implementing the 'clone()' method restricts subclasses from reusing the 'clone()' method of the superclass.

Example

 package MISC;
 
 public class CLNC {
     public Object clone() {
         CLNC cl = new CLNC();    // violation
         // get attributes' clone.
         return cl;
     }
 }

Repair

Use 'super.clone()' instead of calling a constructor to implement 'clone()'.

 public class CLNC {
     public Object clone() {
         CLNC cl = (CLNC) super.clone();
         // get clone for cl's attributes.
         return cl;
     }
 }

Reference

Daconta, M, Monk, E, Keller, J, and Bohnenberger, K. Java Pitfalls. John Wiley & Sons, 2000, pp.12 - 14.


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft